Standard Template Library (STL) - std::queue::empty

Standard Template Library (STL) - std::queue::empty

public member function - 公开成员函数

1. std::queue::empty

bool empty() const;

Test whether container is empty - 测试容器是否为空

检查底层的容器是否为空。

Returns whether the queue is empty: i.e. whether its size is zero.
返回队列是否为空:即队列大小是否为零。

This member function effectively calls member empty of the underlying container object.
该成员函数有效地调用底层容器对象的成员 empty

2. Parameters

none - 无

3. Return value

true if the underlying container’s size is 0, false otherwise.
如果底层容器的大小为 0,则为 true,否则为 false

若底层容器为空则为 true,否则为 false

4. Examples

4.1 std::queue::empty

//============================================================================
// Name        : std::queue::empty
// Author      : Yongqiang Cheng
// Version     : Feb 22, 2020
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>       // std::cout
#include <queue>          // std::queue

int main()
{
	std::queue<int> queue_data;
	int sum(0);

	for (int i = 1; i <= 10; i++)
	{
		queue_data.push(i);
	}

	while (!queue_data.empty())
	{
		sum += queue_data.front();
		queue_data.pop();
	}

	std::cout << "total: " << sum << '\n';

	return 0;
}

The example initializes the content of the queue to a sequence of numbers (form 1 to 10). It then pops the elements one by one until it is empty and calculates their sum.
该示例将队列的内容初始化为一个数字序列 (格式为 1 到 10)。然后,将元素逐一弹出,直到元素为空,然后计算它们的总和。

total: 55

5. Complexity - 复杂度

Constant (calling empty on the underlying container).
常数 (在底层容器上调用 empty)。

6. Data races - 数据竞争

The container is accessed.
容器被访问。

7. Exception safety - 异常安全性

Provides the same level of guarantees as the operation performed on the container (no-throw guarantee for standard container types).
提供与在容器上执行的操作相同级别的保证 (标准容器类型的无抛出保证)。

assignment [ə'saɪnmənt]:n. 任务,布置,赋值

References

http://www.cplusplus.com/reference/queue/queue/empty/
https://en.cppreference.com/w/cpp/container/queue/empty

发布了454 篇原创文章 · 获赞 1733 · 访问量 103万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104453304