欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

队列的基本用法

程序员文章站 2022-03-08 08:05:43
...

转自:https://blog.csdn.net/wangshihui512/article/details/8930652

queue

queue模板类的定义在<queue>头文件中。

与stack模板类很相似,queue模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque类型。

定义queue对象的示例代码如下:

queue<int> q1;

queue<double> q2;

queue的基本操作有:


入队,如例:q.push(x); 将x接到队列的末端。

出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。

访问队首元素,如例:q.front(),即最早被压入队列的元素。

访问队尾元素,如例:q.back(),即最后被压入队列的元素。

判断队列空,如例:q.empty(),当队列空时,返回true。

访问队列中的元素个数,如例:q.size()


std::queue

FIFO queue
queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.

queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front".

The underlying container may be one of the standard container class template or some other specifically designed container class. The only requirement is that it supports the following operations:

  • front()
  • back()
  • push_back()
  • pop_front()

Therefore, the standard container class templates deque and list can be used. By default, if no container class is specified for a particular queue class, the standard container class template deque is used.

In their implementation in the C++ Standard Template Library, queues take two template parameters:
 
template < class T, class Container = deque<T> > class queue;

Where the template parameters have the following meanings:
  • T: Type of the elements.
  • Container: Type of the underlying container object used to store and access the elements.
In the reference for the queue member functions, these same names are assumed for the template parameters.

 


  1. #include <iostream>
  2. #include <queue>
  3. #include <string>
  4. using namespace std;
  5. void test_empty()
  6. {
  7. queue<int> myqueue;
  8. int sum (0);
  9. for (int i=1;i<=10;i++) myqueue.push(i);
  10. while (!myqueue.empty())
  11. {
  12. sum += myqueue.front();
  13. myqueue.pop();
  14. }
  15. cout << "total: " << sum << endl;
  16. }//运行结果: total: 55
  17. void test_pop()
  18. {
  19. queue<int> myqueue;
  20. int myint;
  21. cout << "\nPlease enter some integers (enter 0 to end):\n";
  22. do
  23. {
  24. cin >> myint;
  25. myqueue.push (myint);
  26. } while (myint);
  27. cout << "myqueue contains: ";
  28. while (!myqueue.empty())
  29. {
  30. cout << " " << myqueue.front();
  31. myqueue.pop();
  32. }
  33. }
  34. /********
  35. 运行结果:
  36. Please enter some integers (enter 0 to end):
  37. 512
  38. 605
  39. 420
  40. 517
  41. 532
  42. 0
  43. myqueue contains: 512 605 420 517 532 0
  44. ********/
  45. void test_size()
  46. {
  47. queue<int> myints;
  48. cout << "0. size: " << (int) myints.size() << endl;
  49. for (int i=0; i<5; i++) myints.push(i);
  50. cout << "1. size: " << (int) myints.size() << endl;
  51. myints.pop();
  52. cout << "2. size: " << (int) myints.size() << endl;
  53. }
  54. /****
  55. 运行结果:
  56. 0. size: 0
  57. 1. size: 5
  58. 2. size: 4
  59. ****/
  60. int main()
  61. {
  62. test_empty();
  63. cout<<"\n***********************************************\n";
  64. test_size();
  65. cout<<"\n***********************************************\n";
  66. test_pop();
  67. cout<<"\n***********************************************\n";
  68. queue<string> q;
  69. // insert three elements into the queue
  70. q.push("These ");
  71. q.push("are ");
  72. q.push("more than ");
  73. //cout << "number of elements in the queue: " << q.size()<< endl;
  74. // read and print two elements from the queue
  75. cout << q.front();
  76. q.pop();
  77. cout << q.front();
  78. q.pop();
  79. //cout << "number of elements in the queue: " << q.size()<< endl;
  80. // insert two new elements
  81. q.push("four ");
  82. q.push("words!");
  83. //cout << "\nnumber of elements in the queue: " << q.size()<< endl;
  84. // skip one element
  85. q.pop();
  86. // read and print two elements
  87. cout << q.front();
  88. q.pop();
  89. cout << q.front() << endl;
  90. q.pop();
  91. // print number of elements in the queue
  92. cout << "number of elements in the queue: " << q.size()<< endl;
  93. }
  94. /*******
  95. *运行结果:
  96. total: 55
  97. ***********************************************
  98. 0. size: 0
  99. 1. size: 5
  100. 2. size: 4
  101. ***********************************************
  102. Please enter some integers (enter 0 to end):
  103. 512
  104. 605
  105. 420
  106. 517
  107. 532
  108. 0
  109. myqueue contains: 512 605 420 517 532 0
  110. ***********************************************
  111. These are four words!
  112. number of elements in the queue: 0
  113. Process returned 0 (0x0) execution time : 33.512 s
  114. Press any key to continue.
  115. ********/


阅读更多
相关标签: 队列