C++ STL priority_queue constructor program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: priority_queue class which is a template container adaptor class that provides a restriction of functionality limiting access to the top element of some underlying container type, which is always the largest.
To do: Using the C++ STL priority_queue to constructs a priority_queue that is empty or that is a copy of a range of a base container object or of other priority_queue
To show: How to use the C++ priority queue constructor to constructs a priority_queue that is empty or that is a copy of a range of a base container object or of other priority_queue in C++ programming
// priority_queue constructors
#include <queue>
#include <vector>
#include <deque>
#include <list>
#include <iostream>
using namespace std;
int main(void)
{
// the first member function declares priority_queue with a default vector base container
priority_queue <int> myq1;
cout<<"myq1 = ";
while (!myq1.empty())
{
cout<<myq1.top()<<" ";
myq1.pop();
}
cout<<endl;
// explicitly declares a priority_queue with non-default deque base container
priority_queue <int, deque <int> > myq2;
cout<<"Pushing 12, 15 and 10"<<endl;
myq2.push(12);
myq2.push(15);
myq2.push(10);
cout<<"myq2 = ";
while (!myq2.empty())
{
cout<<myq2.top()<<" ";
myq2.pop();
}
cout<<endl;
// this method of printing out the elements of a priority_queue removes the elements from the priority queue, leaving it empty
cout<<"After printing, myq2 has "<<myq2.size()<<" elements."<<endl<<endl;
// the third member function declares a priority_queue with a vector base container
// and specifies that the comparison function greater is to be used for ordering elements
priority_queue <int, vector<int>, greater<int> > myq3;
cout<<"Pushing 2, 1, 20 and 3"<<endl;
myq3.push(2);
myq3.push(1);
myq3.push(20);
myq3.push(3);
cout<<"myq3 = ";
while (!myq3.empty())
{
cout<<myq3.top()<<" ";
myq3.pop();
}
cout<<endl<<endl;
// the fourth member function declares a priority_queue and initializes it with elements copied from another container:
// first, inserting elements into myq1, then copying myq1 elements into myq4
cout<<"Pushing 100, 400 and 200"<<endl;
myq1.push(100);
myq1.push(400);
myq1.push(200);
priority_queue <int> myq4(myq1);
cout<<"myq4 = ";
while (!myq4.empty())
{
cout<<myq4.top()<<" ";
myq4.pop();
}
cout<<endl<<endl;
// creates an auxiliary vector object vec5 to be used to initialize myq5
vector <int> vec5;
vector <int>::iterator v5_Iter;
cout<<"Pushing using iterator 10, 30, 20, 50, 7 and 21"<<endl;
vec5.push_back(10);
vec5.push_back(30);
vec5.push_back(20);
vec5.push_back(50);
vec5.push_back(7);
vec5.push_back(21);
cout<<"vec5 = ";
for (v5_Iter = vec5.begin(); v5_Iter != vec5.end(); v5_Iter++)
cout<<*v5_Iter<<" ";
cout<<endl<<endl;
// the fifth member function declares and initializes a priority_queue myq5 by copying the range vec5[_First, _Last) from vector vec5
priority_queue <int> myq5(vec5.begin(), vec5.begin() + 2);
cout<<"myq5 = ";
while (!myq5.empty())
{
cout<<myq5.top()<<" ";
myq5.pop();
}
cout<<endl<<endl;
// the sixth member function declares a priority_queue myq6 with a comparison function greater and initializes myq6 by
// copying the range vec5[_First, _Last) from vector vec5
priority_queue <int, vector<int>, greater<int> >
myq6(vec5.begin(), vec5.begin() + 2);
cout<<"myq6 = ";
while (!myq6.empty())
{
cout<<myq6.top()<<" ";
myq6.pop();
}
cout<<endl;
return 0;
}
Output example:
myq1 =
Pushing 12, 15 and 10
myq2 = 15 12 10
After printing, myq2 has 0 elements.
Pushing 2, 1, 20 and 3
myq3 = 1 2 3 20
Pushing 100, 400 and 200
myq4 = 400 200 100
Pushing using iterator 10, 30, 20, 50, 7 and 21
vec5 = 10 30 20 50 7 21
myq5 = 30 10
myq6 = 10 30
Press any key to continue . . .