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

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

public member function - 公开成员函数

1. std::queue::size

size_type size() const;

Return size - 返回容纳的元素数

Returns the number of elements in the queue.
返回队列中的元素数。

Returns the number of elements in the underlying container, that is, c.size().
返回底层容器中元素的数量,即 c.size()

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

2. Parameters

none - 无

3. Return value

The number of elements in the underlying container.
底层容器中的元素数。

Member type size_type is an unsigned integral type.
成员类型 size_type 是无符号整数类型。

4. Examples

4.1 std::queue::size

//============================================================================
// Name        : std::queue::size
// 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;
	std::cout << "0. size: " << queue_data.size() << '\n';

	for (int i = 0; i < 5; i++)
	{
		queue_data.push(i);
	}
	std::cout << "1. size: " << queue_data.size() << '\n';

	queue_data.pop();
	std::cout << "2. size: " << queue_data.size() << '\n';

	return 0;
}

0. size: 0
1. size: 5
2. size: 4

5. Complexity - 复杂度

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

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/size/
https://en.cppreference.com/w/cpp/container/queue/size

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

猜你喜欢

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