Last Updated : 11 Jul, 2025
In C++, vector max_size() is a built-in function used to find the maximum number of elements that can be held by the vector container. In this article, we will learn about the vector max_size() function in C++.
Let's take a look at an example that illustrates this method:
CPP
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
// Max size of vector
cout << v.max_size();
return 0;
}
4611686018427387903
This article covers the syntax, usage, and common examples about the vector max_size() method in C++ STL:
Syntax of Vector max_size()The max_size() method is a member function of std::vector defined in the <vector> header file.
vmax_size();
Parameters:
Return Value:
The below examples demonstrate the working of vector max_size() function in different scenarios:
Check Maximum Size of an Empty Vector C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
// Find the maximum size of the empty vector
cout << v.max_size();
return 0;
}
4611686018427387903Effects of Size and Capacity on max_size()
The below example demonstrates that max_size() is independent of a vector's current size or capacity.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
// Find max_size() for an empty vector
cout << "Empty vector: " << v.max_size() << endl;
// Add some elements to the vector
v.push_back(10);
v.push_back(20);
v.push_back(30);
cout << "After adding elements: "<< v.max_size()
<< endl;
// Reserve additional capacity
v.reserve(100);
cout << "After reserving capacity: " << v.max_size();
return 0;
}
Empty vector: 4611686018427387903 After adding elements: 4611686018427387903 After reserving capacity: 4611686018427387903
Explanation: Even after adding elements or reserving additional capacity, the max_size() remains unchanged, as it is determined by the environment and allocator.\
Modifying Maximum Size of a Vector with Custom AllocatorThe max_size() also depends on the allocator being used. So we can modify it by using a custom allocator:
C++
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class CA : public allocator<T> {
public:
// Restrict max size to 100
size_t max_size() const noexcept {
return 100;
}
};
int main() {
vector<int, CA<int>> v;
// Find maximum size of a vector with custom allocator
cout << v.max_size();
return 0;
}
4611686018427387903Vector max_size() vs capacity()
The vector max_size() and capacity() are sometimes confused as same, but they actually handle different parameters. The below table list some major differences between vector max_size() and capacity() methods:
Feature max_size() capacity() Definition Returns the theoretical maximum number of elements the vector can hold. Returns the number of elements the vector can currently store without reallocation. Depends On System architecture and allocator. Current size and capacity of the vector. Dynamic Change Does not change dynamically. Can grow dynamically when elements are added.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4