//implementable on C++ 11/14/17
Boost::heap::priority_queue
Description:
Boost heap can also be called as a boost priority queue. Priority queues are nothing but queues that keep data/value in order of priority. The library boost:: heap provides several priority queues like binomial heap, skew heap, fibonacci heap, etc. But we generally use binomial heap and normal boost:: heap:: priority_queue because the rest of the heaps just vary in time complexity for specific member function but have the same functionality. The std::priority_queue provides limited functionality whereas the priority queues in boost:: heap provides more functionality and different performance.
Characteristics:
Some of the additional aspects of heap are:-
>Mutability:- The values of priority of certain elements can be modified.
>Iterators:- We can iterate through all elements very easily.
>Mergable:- These are easily merged with efficiency.
>Comparision:- Heaps can be compared with each other for basic equivalence.
Header File Used:
#include<boost/heap/priority_queue.hpp>
Functions:
>begin():-returns an iterator to the first element contained in the priority queue.
>clear():-removes all elements from the priority queue and clears it.
>emplace():-adds a new element to the priority queue, the element is directly constructed in place.
>empty():-is used to check whether the queue is empty or not.
>end():-returns an iterator to the end of the priority queue.
>pop():-removes the top element from the priority queue.
>push():-adds a new element to the priority queue.
>size():-gives the size of the priority queue.
>swap():-swaps any two priority queues which can also be of different sizes as well.
>top():-returns a constant reference to the max element.
Program to demonstrate the use of various functions:
#include <iostream>
#include<boost/heap/priority_queue.hpp>
using namespace boost::heap;
int main()
{
int y;
priority_queue<int>pq1;
priority_queue<int >pq2;
std::cout << "enter any six values to be inserted in pq1\n";
for (int i = 0;i <=5;i++)
{
std::cin >> y;
pq1.push(y);
}
pq2.push(4);
pq2.push(7);
pq2.push(1);
if (!pq2.empty())
{
pq2.swap(pq1);
}
pq2.pop();
std::cout <<"size of pq1 is"<< pq1.size()<<"\n";
std::cout << "size of pq2 is" << pq2.size() << "\n";
pq1.emplace(0);
std::cout << "the elements in pq2 are" << "\n";
for (int i : pq2)
{
std::cout << i << "\n";
}
std::cout << "the elements in pq1 are" << "\n";
for (int i : pq1)
{
std::cout << i << "\n";
}
std::cout <<"top element of pq1 queue is :"<< pq1.top()<<"\n";
std::cout <<"top element of pq2 queue is:"<< pq2.top()<<"\n";
if (pq1 > pq2)
{
std::cout << "pq1 has more elements than pq2";
}
else
{
std::cout << "pq2 has more elements than pq1";
}
return 0;
}
enter any six values to be inserted in pq1
2
4
7
1
9
8
size of pq1 is3
size of pq2 is5
the elements in pq2 are
8
7
4
1
2
the elements in pq1 are
7
4
1
0
top element of pq1 queue is:7
top element of pq2 queue is:8
pq2 has more elements than pq1
Comments