Queue constructor
Syntax:
#include <queue> queue(); queue( const Container& con );
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: Nate Welcome to the line, please enter your name: lizzy Welcome to the line, please enter your name: Robert B. Parker Welcome to the line, please enter your name: ralph Welcome to the line, please enter your name: Matthew Now serving: Nate Now serving: lizzy Now serving: Robert B. Parker Now serving: ralph Now serving: Matthew