fork download
  1. //C++std容器实现queue队列
  2. #include <iostream>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. int main(){
  7. queue<int> q;//创建一个队列,存储int类型元素
  8. //入队操作
  9. q.push(10);
  10. cout<<"入队:10"<<endl;
  11. q.push(20);
  12. cout<<"入队"<<endl;
  13. q.push(30);
  14. cout<<"30"<<endl;
  15. cout<<"访问队列的第一个元素"<<q.front()<<endl;
  16. cout<<"队列最后一个元素"<<q.back()<<endl;
  17.  
  18. //清空队列
  19. while(!q.empty()){
  20. cout<<"移除"<<q.front()<<endl;
  21. q.pop();
  22. }
  23. cout<<"队列已经清空"<<endl;
  24. return 0;
  25.  
  26. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
入队:10
入队
30
访问队列的第一个元素10
队列最后一个元素30
移除10
移除20
移除30
队列已经清空