C++ STL queue, back(), push() and front() 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++ back() to return a reference to the last and most recently added element at the back of the queue, push() to add an element to the back of the queue and front() to return a reference to the first element at the front of the queue

To show: How to use the C++ back() to return a reference to the last and most recently added element at the back of the queue, push() to add an element to the back of the queue and front() to return a reference to the first element at the front of the queue

 

// C++ STL queue, back(), push() and front()

#include <queue>

#include <iostream>

using namespace std;

 

int main(void)

{

// queue container

queue <int> que1;

 

// push/insert data

que1.push(11);

que1.push(13);

 

int& x = que1.back();

const int& y = que1.front();

 

// print data

cout<<"The integer at the back of queue que1 is "<<x<<endl;

cout<<"The integer at the front of queue que1 is "<<y<<endl;

return 0;

}

 

Output examples:

 

The integer at the back of queue que1 is 13

The integer at the front of queue que1 is 11

Press any key to continue . . .

 

 

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