C++队列用法实例

2020-01-06 13:24:27丽君

易采站长站为您分析C++队列用法,实例分析了C++实现队列的入队、出队、读取与判断等相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C++队列用法。。具体如下:

 

 
  1. /*  队列使用时必须包含头文件 #include <queue> 有以下几种方法 
  2. 入队push(),出队pop(), 读取队首元素front(),读取队尾元素back() ,  判断队是否有元素empty() 
  3. 求队列元素个数size()   */ 
  4. #include <iostream>  #include <queue> 
  5. using namespace std;  int main() 
  6. {  queue<int> one; 
  7. one.push(1);  one.push(2); 
  8. one.push(3);  cout<<"one 队列长度:"<<one.size()<<endl; 
  9. cout<<"队尾元素是:"<<one.back()<<endl;  cout<<"队头元素是:"<<one.front()<<endl;  
  10. cout<<"队列是否为空(1为空,0为非空):"<<one.empty()<<endl;  one.pop(); //删除是从队头元素开始的  
  11. cout<<one.front()<<endl;  cout<<one.size()<<endl; 
  12. //cout<<one.top()<<endl; //普通队列好像没有次方法   //优先队列的使用 优先队列中使用back、front 出现错误  
  13. priority_queue<int> three;  three.push(10); 
  14. three.push(20);  three.push(30); 
  15. cout<<"three 优先队列长度:"<<three.size()<<endl;   cout<<"队列是否为空(1为空,0为非空):"<<three.empty()<<endl; 
  16. while (false == three.empty())  { 
  17. cout<<three.top()<<endl;  three.pop(); 
  18. }  cout<<endl; 
  19. system("pause");  return 0;