c ++stl队列使用_在C ++ STL中打印队列的所有元素

朴灿烈づ我的快乐病毒、 2023-03-05 12:58 106阅读 0赞

c ++stl队列使用

To print all elements of a Queue, we follow the following steps:

打印Queue的所有元素,请执行以下步骤:

  1. Run a loop till “queue is not empty”.

    循环运行直到“队列不为空”

  2. Print the first (oldest) element by using queue::front() method

    通过使用queue :: front()方法打印第一个(最旧的)元素

  3. Remove the oldest element (perform “pop” operation to remove the element)

    删除最旧的元素(执行“ pop”操作以删除该元素)

Note: This process will remove all elements of the queue as well. The efficient way is discussed below.

注意:此过程还将删除队列中的所有元素。 有效方法将在下面讨论。

1)正常方式 (1 ) Normal way)

  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. //Main fubction
  5. int main()
  6. {
  7. // declaring an empty queue
  8. queue<int> Q;
  9. //inserting elements
  10. Q.push(10);
  11. Q.push(20);
  12. Q.push(30);
  13. cout<<"Queue size before printing the elements: "<<Q.size()<<endl;
  14. cout<<"Queue element are..."<<endl;
  15. while(!Q.empty()){
  16. cout<<" "<<Q.front();
  17. Q.pop();
  18. }
  19. cout<<endl;
  20. cout<<"Queue size after printing the elements: "<<Q.size()<<endl;
  21. return 0;
  22. }

Output

输出量

  1. Queue size before printing the elements: 3
  2. Queue element are...
  3. 10 20 30
  4. Queue size after printing the elements: 0

Note: See the output, after printing the all elements the size of the Queue is 0 that means all elements have been popped.

注意:查看输出,在打印所有元素后,队列的大小为0,这意味着所有元素都已弹出。

2)高效方式 (2 ) Efficient way)

Here, we can create a copy of the Queue and use that copied queue to print the elements and the another best way is to create a function (using call by value) to print the queue, since we know that function takes the copy of the variable and whatever changes happened with the function variable do not affect to the actual variables.

在这里,我们可以创建Queue的副本,并使用该复制的队列来打印元素,而另一种最佳方法是创建一个函数(使用按值调用)来打印队列,因为我们知道该函数会采用Queue的副本。变量以及函数变量发生的任何变化都不会影响实际变量。

  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. //function to print the queue
  5. void printQueue(queue<int> q)
  6. {
  7. //printing content of queue
  8. while (!q.empty()){
  9. cout<<" "<<q.front();
  10. q.pop();
  11. }
  12. cout<<endl;
  13. }
  14. //Main fubction
  15. int main()
  16. {
  17. // declaring an empty queue
  18. queue<int> Q;
  19. //inserting elements
  20. Q.push(10);
  21. Q.push(20);
  22. Q.push(30);
  23. cout<<"Queue size before printing the elements: "<<Q.size()<<endl;
  24. cout<<"Queue element are... "<<endl;
  25. printQueue(Q);
  26. cout<<"Queue size after printing the elements: "<<Q.size()<<endl;
  27. return 0;
  28. }

Output

输出量

  1. Queue size before printing the elements: 3
  2. Queue element are...
  3. 10 20 30
  4. Queue size after printing the elements: 3

翻译自: https://www.includehelp.com/stl/print-all-elements-of-a-queue.aspx

c ++stl队列使用

发表评论

表情:
评论列表 (有 0 条评论,106人围观)

还没有评论,来说两句吧...

相关阅读

    相关 STL运用

    卡片游戏:很好地介绍了队列的特点和应用 桌上有一叠牌,从第一张牌开始从上往下依次编号1~n。当至少还剩两张牌时进行如下操作:把第一张牌扔掉,然后把新的第一张牌放到整叠