Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Queue in C++

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 293
    Comment on it

    The std::queue class is a container adapter that gives the programmer the functionality of a queue.
    It is a FIFO (first-in, first-out) data structure.
    In queue elements are inserted at the back and can only be accessed from the front and the back.

    A sample demo program using queue

    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    int main()
    {
                queue <int> q; // declaring a queue which will hold integers
                for ( int i = 0; i < 10; i++) {
                        q.push(i); // adding elements to the queue
                }
    
                while (!q.empty()) {
                        cout << "Element at front in queue " << q.front();
                        cout << " " << "Size of queue : " << q.size() << endl;
                        q.pop(); //this will remove element from the front
                }
    
                return 0;
    }
    

    The output of the following program :

    Element at front in queue 0 Size of queue : 10
    Element at front in queue 1 Size of queue : 9
    Element at front in queue 2 Size of queue : 8
    Element at front in queue 3 Size of queue : 7
    Element at front in queue 4 Size of queue : 6
    Element at front in queue 5 Size of queue : 5
    Element at front in queue 6 Size of queue : 4
    Element at front in queue 7 Size of queue : 3
    Element at front in queue 8 Size of queue : 2
    Element at front in queue 9 Size of queue : 1
    

    Member functions

    Element access
    front
    back

    Capacity
    empty
    size

    Modifiers
    push
    emplace
    pop
    swap

    Queue has many real life implementation. For e.g. Circular queue is used in Round Robin Scheduling algorithm which allocates resources to the cpu/processes.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: