stl:queue:queue_constructors

C++ Reference

Queue constructor

Syntax:

    #include <queue>
    queue();
    queue( const queue& other );

Queues have a default constructor as well as a copy constructor that will create a new queue out of the container con.

For example, the following code creates a queue of strings, populates it with input from the user, and then displays it back to the user:

    queue<string> waiting_line;
    while( waiting_line.size() < 5 ) {
      cout << "Welcome to the line, please enter your name: ";
      string s;
      getline( cin, s );
      waiting_line.push(s);
    }
 
    while( !waiting_line.empty() ) {
      cout << "Now serving: " << waiting_line.front() << endl;
      waiting_line.pop();
    }

When run, the above code might produce this output:

Welcome to the line, please enter your name: Bart
Welcome to the line, please enter your name: Milhouse
Welcome to the line, please enter your name: Ralph
Welcome to the line, please enter your name: Lisa
Welcome to the line, please enter your name: Lunchlady Doris
Now serving: Bart
Now serving: Milhouse
Now serving: Ralph
Now serving: Lisa
Now serving: Lunchlady Doris