Standard Template Library (STL) - std::map::max_size

Standard Template Library (STL) - std::map::max_size

public member function - 公开成员函数

1. std::map::max_size

C++98
size_type max_size() const;

C++11
size_type max_size() const noexcept;

Return maximum size - 返回可容纳的最大元素数

Returns the maximum number of elements that the map container can hold.
返回 map 容器可以容纳的最大元素数。

This is the maximum potential size the container can reach due to known system or library implementation limitations, but the container is by no means guaranteed to be able to reach that size: it can still fail to allocate storage at any point before that size is reached.
由于已知的系统或库实现限制,这是容器可以达到的最大潜在大小,但绝不能保证容器能够达到该大小:在达到该大小之前,它仍然无法在任何时候分配存储

返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end())

此值通常反映容器大小上的理论极限,至多为 std::numeric_limits<difference_type>::max()。运行时,可用 RAM 总量可能会限制容器大小到小于 max_size() 的值。

2. Parameters

none - 无

3. Return value

The maximum number of elements a map container can hold as content.
map 容器可以容纳的最大元素数量。

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

4. Examples

4.1 std::map::max_size

Here, member max_size is used to check beforehand whether the map will allow for 1000 elements to be inserted.
在这里,成员 max_size 用于预先检查该 map 是否允许插入 1000 个元素。

//============================================================================
// Name        : std::map::max_size
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <map>

int main()
{
	int i;
	std::map<int, int> map_data;

	if (map_data.max_size() > 1000)
	{
		for (i = 0; i < 1000; i++)
		{
			map_data[i] = 0;
		}

		std::cout << "The map contains 1000 elements.\n";
	}
	else
	{
		std::cout << "The map could not hold 1000 elements.\n";
	}

	return 0;
}

The map contains 1000 elements.

4.2 std::map::max_size

//============================================================================
// Name        : std::map::max_size
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <map>

int main()
{
	std::map<char, char> map_data;
	std::cout << "Maximum size of a 'map' is " << map_data.max_size() << "\n";

	return 0;
}

Maximum size of a 'map' is 461168601842738790

5. Complexity - 复杂度

Constant. - 常数。

6. Iterator validity - 迭代器有效性

No changes.

7. Data races - 数据竞争

The container is accessed. - 容器被访问。

No elements are accessed: concurrently accessing or modifying them is safe.
没有元素被访问:同时访问或修改它们是安全的。

8. Exception safety - 异常安全性

No-throw guarantee: this member function never throws exceptions.
No-throw guarantee:此成员函数从不抛出异常。

References

http://www.cplusplus.com/reference/map/map/max_size/

发布了443 篇原创文章 · 获赞 1685 · 访问量 101万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104227663
今日推荐