In the C++ programming language, the Standard Template Library (STL) offers helpful data structures and containers that help to reduce the complexity of programming. One of the data structure types includes a priority queue, which is a type of container adaptor and allows an ordered retrieval of some data.
A priority queue is a type of container adaptor that looks and behaves like (by default in C++, a max-heap) a heap. In other words, the elements are stored such that their highest internal priority is accessible from an element on top (using the member function top). The priority queue is implemented in C++ as a container adaptor that normally stores the element values in a vector and uses a heap as the design framework.
In C++, the priorityqueue empty function is used to check whether the priorityqueue container is empty or not. If priority_queue is empty, it returns true; otherwise, it returns a false value.
Syntax
It has the following syntax:
pq.empty ();
In this syntax,
- Parameter: It does not take any parameters.
- Return value: It does not return any value.
C++ Simple priority_queue empty function Example
Let us take an example to illustrate the priority_queue empty function in C++.
Example
#include <iostream>
#include <queue>
using namespace std; //using standard namespace
int main() //main function
{
priority_queue<char> mp;
mp.push('a');
mp.push('b');
mp.push('c');
if(mp.empty())
{
cout<< "true";
}
else
{
cout<< "false";
}
return 0;
}
Output:
Explanation:
In this example, we have created a priority_queue of characters that contains three elements 'a', 'b', and 'c' are inserted using the push function. After that, we use the empty function to check whether the priority queue is empty or not. Since the elements have been added, the condition returns false, which shows that the priority queue contains elements.
C++ Example to check whether a Priority Queue is Empty Using the empty Function
Let us consider an example to illustrate how to check whether a priority queue is empty using the empty function in C++.
Example
#include <iostream>
#include <queue>
using namespace std; //using standard namespace
int main() //main function
{
priority_queue<int> mp;
priority_queue<int> mp1;
mp.push(1);
mp.push(2);
if(mp.empty())
{
cout<< "true";
}
else
{
cout<< "false \n";
}
if(mp1.empty())
{
cout<< "true";
}
else
{
cout<< "false";
}
return 0;
}
Output:
false
true
Explanation:
In this example, we have created two priority_queue objects, mp and mp1, where elements 1 and 2 are inserted into mp, while mp1 remains empty. After that, we use the empty function to check if the queues contain any elements. For the mp object, it returns a false value if it contains any element. For the mp1 object, it returns a true value if it is empty.
C++ Example using priority_queue with User-Defined Data Type
Let us take an example to illustrate the priority_queue with the user-defined data types using the empty function in C++.
Example
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Job {
int priority;
string name;
bool operator<(const Job& j) const {
return priority < j.priority;
}
};
int main() {
priority_queue<Job> jobs;
jobs.push({3, "Task A"});
jobs.push({1, "Task B"});
jobs.push({5, "Task C"});
cout << "Processing jobs in priority order:\n";
while (!jobs.empty()) {
cout << jobs.top().name << " (Priority: " << jobs.top().priority << ")\n";
jobs.pop();
}
cout << (jobs.empty() ? "All jobs processed!" : "Jobs remaining!") << endl;
}
Output:
Processing jobs in priority order:
Task C (Priority: 5)
Task A (Priority: 3)
Task B (Priority: 1)
All jobs processed!
Explanation:
In this example, we have created a structure called 'Job' that has a name and a priority. After that, we add a number of jobs to a 'priority_queue'. As we have overloaded the comparison operator '<', the priority queue will automatically sort jobs such that the highest priority will always be on top. Finally, we enter a loop that will print and remove the top job of the queue until the queue is empty, and then we print a message that all jobs are done.
Features of the priority_queue empty function in C++
There are several features of the priority_queue empty function in C++. Some of them are as follows:
- The priority_queue in C++ is commonly utilized to check whether the queue is empty or not.
- When we use this function in the program, it does not throw any exceptions, which provides a safe environment to use.
- It takes only constant time complexity O(1) to perform several operations, which means that it executes the tasks in constant time.
- This function is commonly utilized in the C++ program before accessing or popping the elements to avoid operations on an empty priority queue.
- It returns the Boolean value. It returns a true value if the priority_queue is empty. Otherwise, it returns a false value.
Conclusion
In conclusion, the C++ priorityqueue::empty function is a simple and essential method in the priorityqueue container. It is commonly utilized to check whether the priority queue is empty or not. It is very useful to prevent undefined behavior and ensure safe, expected, and efficient behavior when using heap-based data structures. It returns a Boolean value when we perform any operation. It returns a true value if the priority queue is empty; otherwise, it returns a false value. Overall, the priority_queue empty function helps to ensure program stability and prevents runtime errors when working with priority queues.
C++ Priority_queue empty function FAQs
1) What does the empty function do for a priority_queue in C++?
In C++, the empty method is commonly utilized to check whether the priorityqueue has any elements. It returns a true value if the container has no elements; otherwise, it returns a false value. The empty function can avoid potential problems that may occur when we call the pop or top functions on an empty priorityqueue.
2) What is the time complexity of the empty function in C++?
The C++ empty function takes only constant time complexity O(1) to perform any operation.
3) Does calling the empty function remove elements from the priority_queue in C++?
No, calling the empty function does not remove any elements from the queue. It simply returns whether or not the underlying container has any elements in it. The method to remove elements from the priority_queue is pop.
4) Is it safe to call top or pop without calling empty first?
No, it is not safe. If we call the top or pop functions on an empty priority_queue, the behavior is undefined, which can result in any number of runtime errors or crashing.
We should always check:
if (!pq.empty()) pq.pop();
5) Are pq.empty and pq.size == 0 the same in C++?
Yes, both functions will return true when the container is empty. However, the pq.empty function should generally be preferred for better clarity and readability. It indicates to the programmer the intent to check that the container is empty rather than counting container elements.