Home »
C++ STL
vector::max_size() function with example in C++ STL
C++ STL vector::max_size() function: Here, we are going to learn about the max_size() function of vector header in C++ STL with example.
Submitted by IncludeHelp, on May 13, 2019
C++ vector::max_size() function
vector::max_size() is a library function of "vector" header, it is used to get the maximum size of a vector, it returns the total number of elements that a vector can store.
Note: To use vector, include <vector> header.
Syntax
Syntax of vector::max_size() function
vector::max_size();
Parameter(s)
none – It accepts nothing.
Return value
size_type – It returns the maximum size of a vector as an unsigned integral type.
Sample Input and Output
Input:
vector<int> vector1{ 1, 2, 3, 4, 5 };
Function call:
cout << vector1.max_size();
Output:
4611686018427387903
C++ program to demonstrate example of vector::max_size() function
//C++ STL program to demonstrate example of
//vector::max_size() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
//printing the max_size of the vector
cout << "Maximum number of elements that can be stored: ";
cout << v1.max_size() << endl;
//pushing elements
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//printing the max_size of the vector
cout << "Maximum number of elements that can be stored: ";
cout << v1.max_size() << endl;
return 0;
}
Output
Maximum number of elements that can be stored: 4611686018427387903
Maximum number of elements that can be stored: 4611686018427387903
Reference: C++ vector::max_size()