Home C++ Vectors Functions capacity() C++ std::vector capacity() Function Definition In C++, std::vector::capacity() tells you how many elements the vector can store in its current memory block before it must allocate a larger one.
size() is how many elements you have now. capacity() is how many would fit without a reallocation. Include <vector> to use it.
This is a member of std::vector<T>, a dynamic contiguous array . Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.
Syntax Call capacity with empty brackets. It takes no arguments.
std::size_t cap = v.capacity();
std::cout << v.size() << " / " << v.capacity(); Call What it does v.capacity()Returns the current storage size in elements. Always >= v.size().
Use reserve(n) to raise capacity in advance. Use shrink_to_fit() to ask the library to free spare room (it is a non-binding request).
Library declarations size_type capacity() const noexcept; Parameters Parameter Description None capacity() takes no parameters. Call it with empty brackets.
Return Value Return Value Description size_typeThe number of elements that fit without reallocating. Often larger than size().
Important
When size() reaches capacity() and you add another element, the vector reallocates. That move can invalidate all iterators, references, and pointers . Prefer reserve() when you know the final size.
C++ Examples 1. size vs capacity 2. After reserve 3. Two vectors Example 1 — Print size() and capacity() See that capacity can be larger than size after several push_back calls.
#include <iostream>
#include <vector>
int main() {
std::vector<int> v;
for (int i = 0; i < 5; ++i) {
v.push_back(i);
std::cout << "size=" << v.size()
<< " capacity=" << v.capacity() << '\n';
}
return 0;
} size=1 capacity=1
size=2 capacity=2
size=3 capacity=4
size=4 capacity=4
size=5 capacity=8 How It Works The vector starts empty. Each push_back() grows size() by one. When size would exceed capacity, the vector allocates a bigger block (often about double). Capacity then jumps ahead of size. Exact capacity values depend on the library, but the idea is the same: spare room avoids reallocating on every insert. Example 2 — reserve() Raises Capacity Ask for room for 100 elements, then add a few and show capacity stays high.
#include <iostream>
#include <vector>
int main() {
std::vector<int> v;
v.reserve(100);
v.push_back(1);
v.push_back(2);
v.push_back(3);
std::cout << "size=" << v.size() << '\n';
std::cout << "capacity=" << v.capacity() << '\n';
return 0;
} How It Works reserve(100) asks the vector to prepare storage for at least 100 elements.Three push_back() calls only change the size. Capacity stays at least 100, so no reallocation happened. This pattern is common when you know how many elements you will add. Example 3 — Same Size, Different Capacity Two vectors can hold the same count of elements but report different capacities.
#include <iostream>
#include <vector>
int main() {
std::vector<int> a = {1, 2, 3};
std::vector<int> b;
b.reserve(50);
b = {1, 2, 3};
std::cout << "a size=" << a.size() << " cap=" << a.capacity() << '\n';
std::cout << "b size=" << b.size() << " cap=" << b.capacity() << '\n';
return 0;
} a size=3 cap=3
b size=3 cap=50 How It Works Vector a was built from a short list, so its capacity is often equal to its size. Vector b reserved 50 slots first, then got three values. Size is 3, but capacity stays at least 50. Both store the same elements. Capacity is about reserved memory, not about the logical length. Frequently Asked Questions What does std::vector::capacity() return? It returns how many elements fit in the current storage without a reallocation. It is always at least size().
What is the difference between size() and capacity()? size() is the number of elements you use. capacity() is the number of elements the current block can hold. Capacity can be larger than size.
Does capacity() change after clear()? clear() sets size to 0, but capacity usually stays the same. The memory block is kept for later push_back calls.
How do I raise capacity? Call reserve(n). It makes capacity at least n without changing size.
Do array and deque have capacity()? std::array has a fixed size and no capacity(). std::deque has no capacity() or reserve() either. Those members are special to vector (and string).
Next: cbegin() Get a read-only iterator to the first element with std::vector.
cbegin() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
13 people found this page helpful
Helpful Share Copy link Suggestion