Suman Vajjala
Suman Vajjala

Reputation: 217

maximum no of integer items that can be created by an int vector

I have an integer vector

std::vector<int> somevec

The limits of int can be queried via

std::numeric_limits<int>::min() and std::numeric_limits<int>::max()

Can I create a vector whose size exceeds std::numeric_limits::max() i.e.

can somevec.size() > std::numeric_limits<int>::max()

Upvotes: 2

Views: 234

Answers (1)

Pubby
Pubby

Reputation: 53097

std::vector uses a size_type member for indexing, which is usually not the same type as int. Thus, use:

std::numeric_limits<std::vector<int>::size_type>::max()

So in theory, yes, it's possible to have somevec.size() be larger than std::numeric_limits<int>::max().

However, std::vector also has a maximum size which is usually smaller than this amount, you can query it like this:

somevec.max_size();

Upvotes: 5

Related Questions