std::array max_size() 方法
- 自 C++11 起
constexpr size_type max_size() const noexcept;
返回容器由于系统或库实现限制而能够容纳的最大元素数量,即最大容器的 std::distance(begin(), end())
。
参数
(无)
返回值
容器可以容纳的最大元素数量。
复杂度
常数。
备注
重要
因为每个 std::array<T, N>
都是一个固定大小的容器,所以 max_size 返回的值等于 N
(这也是 size
返回的值)。
示例
Main.cpp
#include <iostream>
#include <locale>
#include <array>
int main()
{
std::array<char, 10> q;
std::cout.imbue(std::locale("en_US.UTF-8"));
std::cout << "Maximum size of the std::array is " << q.max_size() << '\n';
}
可能输出
Maximum size of the std::array is 10