CPP STL Tutorial : C++ std::queue and it’s operations.

In this chapter we shall learn about:
1. queue introduction.
2. queue operations.
3. queue member declaration.
4. queue member functions

1. queue introduction.

1. queue is a type of container adapter.
2. It supports FIFO [First In First Out].
3. FIFO means, that the elements are inserted first will be removed first.
Below is the header file to be used for stack:
#include <queue>          // std::stack

2. queue operations.

queue will support below operations:
empty
size
front
back
push_back
pop_front

3. queue member declaration.

std::queue<int> myqueue;
myqueue.push (1);
myqueue.push (2);
myqueue.push (3);
myqueue.push (4);

4. queue member functions

empty : It will test whether container is empty
size : It will return size
front : It will access next element
back : It will access last element
push : It will insert element
pop : It will remove element
#include <iostream>
#include <queue>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main ()
{

  std::queue<int> myqueue;
	myqueue.push (1);
	myqueue.push (2);
	myqueue.push (3);
	myqueue.push (4);
  
  cout<<"Size of queue is "<< myqueue.size()<<endl;
   std::cout << "myqueue.front()" << myqueue.front() << '\n';
   std::cout << "myqueue.back()" << myqueue.back() << '\n';
  cout<<"queue elements are: "<<endl;

   while (!myqueue.empty())
  {
     std::cout << ' ' << myqueue.front();
     myqueue.pop();
  }
  return 0;
}
Output:
Size of queue is 4
myqueue.front() 1
myqueue.back() 4
queue elements are:
1 2 3 4
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *