C++ Program to Illustrate Usage of Queue

This C++ program illustrates usage of queue. The std::queue class is a container adapter that gives the programmer the functionality of a queue – specifically, a FIFO (first-in, first-out) data structure. The class template acts as a wrapper to the underlying container – only a specific set of functions is provided. The queue pushes the elements on the back of the underlying container and pops them from the front.

Here is the source code of the C++ program which illustrates usage of queue. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to illustrate usage of queue container
  3.  */
  4. #include <iostream>
  5. #include <queue>
  6. #include <string>
  7. using namespace std;
  8.  
  9. int main() {
  10.     queue<string> q;
  11.  
  12.     q.push(string("George"));
  13.     q.push(string("John"));
  14.     q.push(string("Roger"));
  15.     q.push(string("Fred"));
  16.     cout << "The queue consists of " << q.size() << " people.\n";
  17.     cout << q.front() << " is at the front of the queue.\n";
  18.     cout << q.back() << " is at the back of the queue.\n"
  19. }

$ gcc test.cpp
$ a.out
The queue consists of 4 people.
George
John
Roger
Fred

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.