Skip to content
Related Articles

Related Articles

Improve Article
vector max_size() function in C++ STL
  • Difficulty Level : Basic
  • Last Updated : 10 Jul, 2018

The vector::max_size() is a built-in function in C++ STL which returns the maximum number of elements that can be held by the vector container.

Syntax:

vector_name.max_size()

Parameters: The function does not accept any parameters.

Return value: The function returns the maximum numbers that can fit into the vector container.

Program below illustrates the above function:




// C++ program to illustrate the
// vector::max_size() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialize a vector
    vector<int> vec;
  
    // returns the max_size of vector
    cout << "max_size of vector 1 = " << vec.max_size() << endl;
  
    vector<int> vec1;
  
    // returns the max_size of vector
    cout << "max_size of vector 2 = " << vec1.max_size() << endl;
    return 0;
}
Output:
max_size of vector 1 = 4611686018427387903
max_size of vector 2 = 4611686018427387903
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :