//implementable on C++ 11/14/17

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.

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.

#include<boost/heap/priority_queue.hpp>

>**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.

using namespace boost::heap;
int main()
{
int y;
priority_queue<int>pq1;//a boost::heap::priority_queue
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;//values that we want to push in the priority queue
pq1.push(y);//to push value in priority queue
}
pq2.push(4);
pq2.push(7);
pq2.push(1);
if (!pq2.empty())//checks whether pq2 is empty or not
{
pq2.swap(pq1);//used to swap the two priority queue, indirectly all elements of pq1 becomes that of pq2 and visa-versa
}
pq2.pop();//top element of priority queue gets popped out which is basically the largest element in pq2
std::cout <<"size of pq1 is"<< pq1.size()<<"\n";//to print the size of the priority queue
std::cout << "size of pq2 is" << pq2.size() << "\n";
pq1.emplace(0);//to add element to the priority queue which is constructed in place
std::cout << "the elements in pq2 are" << "\n";
for (int i : pq2)//used to iterate over the elements which the std::priority_queue does not provide
{
std::cout << i << "\n";//prints the elements in random order
}
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";//prints the element at the top of the queue
std::cout <<"top element of pq2 queue is:"<< pq2.top()<<"\n";
if (pq1 > pq2)//we can check which queue has more number of elements in size by using relational operator only in boost::heap and if they have same number of elements ,then elements will be compared in pairs
{
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

Name | Views | Likes |
---|---|---|

C++ Boost Any | 1717 | 10 |

C++ boost::heap::binomial_heap | 1196 | 10 |

C++ Boost Scope Exit | 6747 | 8 |

C++ Boost Bind | 1595 | 6 |

C++ Boost Compressed Pair | 1370 | 10 |

C++ Boost Dynamic Bitset | 2958 | 10 |

C++ boost::heap::priority_queue | 1315 | 10 |

C++ Program to Implement Hash Tables | 5295 | 9 |

## Comments