C++ queue usage example

  • 2020-04-02 03:10:47
  • OfStack

This article illustrates the use of C++ queues. Share with you for your reference. The details are as follows:



#include <iostream>
#include <queue>
using namespace std;
int main()
{
  queue<int> one;
  one.push(1);
  one.push(2);
  one.push(3);
  cout<<"one  Queue length: "<<one.size()<<endl;
  cout<<" The elements at the end of the queue are: "<<one.back()<<endl;
  cout<<" The header element is: "<<one.front()<<endl; 
  cout<<" Is the queue empty (1 Is empty, 0 Is not empty ) : "<<one.empty()<<endl;
  one.pop(); //The deletion begins with the header element
  cout<<one.front()<<endl;
  cout<<one.size()<<endl;
  //cout<<one.top()<<endl; // There seems to be no secondary method for a normal queue  
  //Use of priority queue an error occurred using back, front in the priority queue
  priority_queue<int> three;
  three.push(10);
  three.push(20);
  three.push(30);
  cout<<"three  Priority queue length: "<<three.size()<<endl;  
  cout<<" Is the queue empty (1 Is empty, 0 Is not empty ) : "<<three.empty()<<endl;
  while (false == three.empty())
  {
     cout<<three.top()<<endl;
     three.pop();
  }
  cout<<endl;
  system("pause");
  return 0; 
}

Hope that the article described in the C++ programming to help you.


Related articles: