std::vector max_size() 方法
- 自 C++20 起
- 自 C++11 起
- 直到 C++11
// Const version only
constexpr bool max_size() const noexcept;
// Const version only
bool max_size() const noexcept;
// Const version only
bool max_size() const;
返回容器由于系统或库实现限制而能够容纳的最大元素数量,即最大容器的 std::distance(begin(), end())
。
参数
(无)
返回值
容器可以容纳的最大元素数量。
复杂度
常数 - O(1)。
备注
此值通常反映容器大小的理论限制,最大为 std::numeric_limits<difference_type>::max()
。在运行时,容器的大小可能会因可用 RAM 量而限制在小于 max_size()
的值。
示例
Main.cpp
#include <iostream>
#include <locale>
#include <array>
int main()
{
std::vector<char> 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 a std::vector is 9,223,372,036,854,775,807