stl:queue 源码_C ++ STL中的queue :: empty()和queue :: size()

迈不过友情╰ 2023-03-05 12:58 113阅读 0赞

stl:queue 源码

In C++ STL, Queue is a type of container that follows FIFO (First-in-First-out) elements arrangement i.e. the elements which inserts first will be removed first. In queue, elements are inserted at one end known as “back” and are deleted from another end known as “front”.

在C ++ STL中,队列是遵循FIFO ( 先进先出 )元素排列的一种容器,即,首先插入的元素将被首先删除。 在队列中,元素被插入称为“ back”的一端,并从称为“ front”的另一端删除。

1)C ++ STL queue :: empty()函数 (1) C++ STL queue::empty() function)

empty() function checks weather a queue is an empty queue or not, if a queue is empty i.e. it has 0 elements, the function returns 1 (true) and if queue is not empty, the function returns 0 (false).

empty()函数检查队列是否为空队列,如果队列为空即具有0个元素,则该函数返回1(true),如果队列不为空,则该函数返回0(false)。

Syntax:

句法:

  1. queue_name.empty()

Parameters(s): None

参数:

Return type:

返回类型:

  1. Returns 1, if queue is empty

    如果队列为空,则返回1

  2. Returns 0, if queue is not empty

    如果队列不为空,则返回0

Program:

程序:

  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. //Main fubction
  5. int main()
  6. {
  7. // declaring two queues
  8. queue<int> Q1;
  9. queue<int> Q2;
  10. //inserting elements to Q1
  11. Q1.push(10);
  12. Q1.push(20);
  13. Q1.push(30);
  14. //checking
  15. if(Q1.empty())
  16. cout<<"Q1 is an empty queue"<<endl;
  17. else
  18. cout<<"Q1 is not an empty queue"<<endl;
  19. if(Q2.empty())
  20. cout<<"Q2 is an empty queue"<<endl;
  21. else
  22. cout<<"Q2 is not an empty queue"<<endl;
  23. return 0;
  24. }

Output

输出量

  1. Q1 is not an empty queue
  2. Q2 is an empty queue

2)C ++ STL queue :: size()函数 (2) C++ STL queue::size() function)

size() returns the total number of elements of a queue or we can say it returns the size of a queue.

size()返回队列中元素的总数,或者可以说它返回队列的大小。

Syntax:

句法:

  1. queue_name.size()

Parameter(s): None

参数:

Return: total number of elements/size of the queue

返回:元素总数/队列大小

Program:

程序:

  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. //Main fubction
  5. int main()
  6. {
  7. // declaring two queues
  8. queue<int> Q1;
  9. queue<int> Q2;
  10. //inserting elements to Q1
  11. Q1.push(10);
  12. Q1.push(20);
  13. Q1.push(30);
  14. cout<<"size of Q1: "<<Q1.size()<<endl;
  15. cout<<"size of Q2: "<<Q2.size()<<endl;
  16. return 0;
  17. }

Output

输出量

  1. size of Q1: 3
  2. size of Q2: 0

翻译自: https://www.includehelp.com/stl/queue-empty-and-queue-size.aspx

stl:queue 源码

发表评论

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

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

相关阅读

    相关 C++ queue(STL queue)

    只能访问 queue<T> 容器适配器的第一个和最后一个元素。只能在容器的末尾添加新元素,只能从头部移除元素。 许多程序都使用了 queue 容器。queue 容器可

    相关 STL:priority_queue

    优先队列完全以底部容器为依据,加上heap处理规则,实现很简单。缺省下以vector为底部容器,利用大根堆。它是一个有权值概念的queue(头出尾进,无其他存取元素的途径)。

    相关 STLqueue

    queue是先进先出(FIFO)的数据结构,有两个口:允许从尾部加入元素、取得头部的元素。没有其他任何方法可以存取queue的其它元素,即queue不允许遍历行为。 SGI

    相关 STL-queue

    queue只能访问queue容器适配器的第一个和最后一个元素。只能在容器的末尾添加新元素,只能从头部移除元素。(先进先出) 1.初始化 需要头文件 queueque;