×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

Queue in C++ Standard Template Library (STL)

C++ STL Queue: Here, we are going to learn about Queue in C++ Standard Template Library (STL) and its basic functions.
Submitted by Radib Kar, on September 27, 2018

C++ provides the awesome feature of STL where we can use the basic ADTs without knowing the code behind the implementations. STL helps a lot to use ADTs efficiently without actually implementing them. This article provides basic knowledge to represent a Queue using C++ STL.

Queue in C++ STL

A queue is the data structure where the order of elements is important and the queue is maintained as FIFO (First In First Out).

Queue operations

The basic ADT operations for queue are...

  1. EnQueue (int x): to enqueuer an element at the rear end
  2. int DeQueue(): to delete an element from the front end and returns the element deleted
  3. bool empty(): a Boolean function to check whether the queue is empty or not
  4. int size(): returns no of elements presented in the queue

The basic ADT operations for queue are available in the STL.

C++ STL - Queue Functions

Queue is the container and the available functions for implanting the ADT operations are mentioned following:

  1. empty() – Returns 1 if the queue is empty else 0
  2. size() – Returns the size of the queue
  3. front() – Returns a reference to the first element of the queue (front end)
  4. back() – Returns a reference to the last element of the queue (rear end)
  5. push(g) – Adds the element ‘g’ at the end of the queue (conventional EnQueue(g) ADT operation)
  6. pop() – Deletes the first element of the queue ( similar to DeQueue(), but it only deletes, doesn’t return anything.)

So to complete the DeQueue operation we need to write

  1. int item= front();
  2. pop();
  3. return item;

Declaring a C++ STL Queue

The following is the syntax to declare queue in C++ STL:

queue<datatype> queue_name;

Example:

queue<int> q; //a queue of integers

Example of Queue in C++ STL

#include <iostream>
#include <queue>  // include header file for STL

using namespace std;

int main() {
  queue<int> q;
  int x, item;

  cout << "enter integers to EnQueue and 0 to stop EnQuing" << endl;
  cin >> x;

  while (x) {
    q.push(x);  // push(int x) function for EnQueueing
    cin >> x;
  }

  cout << "The size of the queue is  : " << q.size() << endl;  // size()
                                                               // function
  cout << "The first element that entered the queue is : " << q.front()
       << endl;  // front() function
  cout << "The last element that entered the queue is : " << q.back()
       << endl;  // back() function

  cout << "DeQueuing......." << endl;
  while (!q.empty()) {
    item = q.front();  // DeQueing
    cout << item << " ";
    q.pop();
  }

  cout << "\n executed successfully\n";

  return 0;
}

Output

enter integers to EnQueue and 0 to stop EnQuing
12
4
45
3
6
7
0
The size of the queue is  : 6
The first element that entered the queue is : 12
The last element that entered the queue is : 7
DeQueuing.......
12 4 45 3 6 7
executed successfully

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.