This question got me thinking about the max_size
method in vector
class. It is quite apparent that practically the number of elements contained in the vector will be much lesser than what max_size
returns. So I was wondering where this will be useful? Any clues?
The set::max_size() is a built-in function in C++ STL which returns the maximum number of elements a set container can hold. Syntax: set_name.max_size() Parameters: This function does not accept any parameters. Return Value: This function returns the maximum number of elements a set container can hold.
max_size() is the theoretical maximum number of items that could be put in your vector. On a 32-bit system, you could in theory allocate 4Gb == 2^32 which is 2^32 char values, 2^30 int values or 2^29 double values.
The C++ function std::vector::capacity() returns the size of allocate storage, expressed in terms of elements. This capacity is not necessarily equal to the size of vector. It can be equal or greater than vector size. The theoretical limit on vector size is given by member max_size.
To get the size of a C++ Vector, you can use size() function on the vector. size() function returns the number of elements in the vector.
It really isn't very useful.
The only theoretical usage would be to check that if you need a container larger than max_size()
, you are in trouble. But you would probably realize that already when considering a port of your database server to a microwave oven.
The committee once considered improving the function, but didn't find it useful enough to be worth a change:
max_size()
isn't useful for very many things, and the existing wording is sufficiently clear for the few cases that max_size() can be used for. None of the attempts to change the existing wording were an improvement.
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#197
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With