vector::max_size function


The vector max_size returns the maximum number of elements the vector can hold.

size_t max_size( ) const;

Parameters:
none

Return type
size_t -The largest possible size of the vector.

The value return by this function is compiler and machine dependent.Different compiler and machine will give different value.

Code example

vector<int>vec ;
cout<< vec.max_size( ) << endl ;

vector<string>vecSt ;
cout<< vecSt.max_size( ) << endl ;

vector<float>vecF;
cout << vecF.max_size( ) << endl;

vector<double>vecD ;
cout << vecD.max_size( ) << endl ;

In my machine 64 bits,Windows 10 while running the code given above in Code::blocks(MinGw 6.1) and Visual Studio 2015,I get the output as shown below.

Output in Code::blocks

4611686018427387903
576460752303423487
4611686018427387903
2305843009213693951

Output in Visual Studio

1073741823
153391689
1073741823
536870911

Although the max_size output is different in Code::blocks and Visual Studio for different types,you might notice certain symmetry in their output.The type having larger size gives smaller max_size value,for instance max_size of int and float is larger than the max_size of double in CB and VS output.Since double size is 8 bytes,but int and float size is 4 bytes,the smaller max_size of double shows the smaller amount of double elements a vector can hold.Since a type with larger size occupy larger space a smaller number of elements is rendered in a fix size memory system.


 


Stop wasting time,earn money($$$) from your website-Join Now!