C++ STL  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:

To do: Using the C++ queue constructor to construct a queue that is empty or that is a copy of a base container object in C++ programming

To show: How to use the C++ queue, constructor to construct a queue that is empty or that is a copy of a base container object in C++ programming

 

// C++ STL queue, constructor

#include <queue>

#include <vector>

#include <list>

#include <iostream>

using namespace std;

 

int main(void)

{

// declares queue with default deque base container

queue <char> qu1;

// explicitly declares a queue with deque base container

queue <char, deque<char> > qu2;

// these lines don't cause an error, even though they declares a queue with a vector base container

queue <int, vector<int> > qu3;

qu3.push(10);

 

// but the following would cause an error because vector has no pop_front() member function

//

// qu3.pop( );

//

// declares a queue with list base container

queue <int, list<int> > qu4;

// the second member function copies elements from a container

list<int> li1;

li1.push_back(3);

li1.push_back(7);

li1.push_back(2);

li1.push_back(9);

queue <int, list<int> > qu5(li1);

cout<<"The element at the front of queue qu5 is "<<qu5.front()<<endl;

cout<<"The element at the back of queue qu5 is "<<qu5.back()<<endl;

return 0;

}

 

Output examples:

 

The element at the front of queue qu5 is 3

The element at the back of queue qu5 is 9

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index