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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章