int向量可以创建的最大no整数项

Sum*_*ala 2 c++

我有一个整数向量

std::vector<int> somevec
Run Code Online (Sandbox Code Playgroud)

可以通过查询int的限制

std::numeric_limits<int>::min() and std::numeric_limits<int>::max()
Run Code Online (Sandbox Code Playgroud)

我可以创建一个大小超过std :: numeric_limits :: max()的向量

can somevec.size() > std::numeric_limits<int>::max()
Run Code Online (Sandbox Code Playgroud)

Pub*_*bby 5

std::vector使用size_type成员进行索引,通常与索引的类型不同int.因此,使用:

std::numeric_limits<std::vector<int>::size_type>::max()
Run Code Online (Sandbox Code Playgroud)

所以理论上,是的,它可能somevec.size()比大于std::numeric_limits<int>::max().

但是,std::vector最大大小通常小于此数量,您可以像这样查询:

somevec.max_size();
Run Code Online (Sandbox Code Playgroud)