Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical use of vector::max_size

Tags:

c++

stl

vector

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?

like image 323
Asha Avatar asked Mar 26 '12 05:03

Asha


People also ask

What is Max_size?

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.

What is the max size of vector?

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.

What is capacity in vector in C++?

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.

How do I find the length of a vector in C++?

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.


1 Answers

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

like image 193
Bo Persson Avatar answered Oct 14 '22 07:10

Bo Persson