CKTIS / C++
std::vector::size — number of elements in a vector
`size()` returns the current element count in constant time. Use that count, not `capacity()`, to determine which indices are valid.
Version: ISO C++23; implementation support may vary
Syntax
// Header: <vector>
// Member declaration inside std::vector<T, Allocator> (ISO C++23)
constexpr size_type size() const noexcept;
// Call on a vector v:
auto count = v.size();std::vector::size is a no-argument member function declared in <vector>. It returns by value the number of elements in [v.begin(), v.end()) (zero for an empty vector). It takes constant time, O(1), and can be called on a const vector. It has been noexcept since C++11 and constexpr since C++20, so it can be used in a constant expression when the surrounding evaluation permits it. This entry describes ISO C++23; implementation support may vary.
capacity() tells you how many elements the vector can hold without reallocating. A successful reserve(n) ensures capacity for at least n elements but creates none. resize(n) sets the element count to n, and a successful push_back increases it by one. If you only need to test whether the vector is empty, use v.empty().
The return type is size_type, an unsigned integer type that need not be std::size_t. Use auto to preserve it rather than converting blindly to int. Valid indices satisfy 0 <= i < v.size() in mathematical notation; that is not a C++ condition. Before converting an index to size_type, reject negative values and values greater than the maximum of size_type. Otherwise, even a nonnegative value from a wider integer type can convert to a different index that happens to be in bounds. v[i] requires a valid index; v.at(i) checks the index after conversion to size_type and throws std::out_of_range if the converted index is greater than or equal to v.size(). Do not compute v.size() - 1 on an empty vector. If you need a signed count, use std::ssize(v) from <iterator> (since C++20).
For a vector with an element type other than bool, [v.data(), v.data() + v.size()) spans the existing elements; do not dereference the end pointer. std::vector<bool> also counts elements, but does not expose such a bool* buffer. A saved count does not update when the vector changes. Reallocation invalidates pointers, references, and iterators to elements, as well as the old end(). Appending invalidates the old end() even without reallocation.
Examples
#include <iostream>
#include <vector>
int main() {
std::vector<int> v;
const auto before = v.size();
v.reserve(8);
std::cout << "after reserve: " << v.size() << '\n';
v.push_back(10);
v.push_back(20);
std::cout << "after append: " << v.size() << '\n';
std::cout << "saved size: " << before << '\n';
v.resize(1);
std::cout << "after resize: " << v.size() << '\n';
}
Result
after reserve: 0 after append: 2 saved size: 0 after resize: 1
After `reserve(8)`, the vector is still empty. Two calls to `push_back` bring the size to 2, but `before` remains 0. `resize(1)` removes the last element. The program assumes no exact capacity.
Related lessons
- C++ · Lesson 11 · Advanced · Version: 3
- C++ · Lesson 14 · Advanced · Version: 3
- C++ · Lesson 15 · Advanced · Version: 3
- C++ · Lesson 18 · Advanced · Version: 3
- C++ · Lesson 23 · Advanced · Version: 3
- C++ · Lesson 24 · Advanced · Version: 3
- C++ · Lesson 27 · Advanced · Version: 3
- C++ · Lesson 29 · Advanced · Version: 3
- C++ · Lesson 30 · Advanced · Version: 3
- C++ · Lesson 32 · Advanced · Version: 3
- C++ · Lesson 34 · Advanced · Version: 3
- C++ · Lesson 51 · Advanced · Version: 3
- C++ · Lesson 54 · Advanced · Version: 3
- C++ · Lesson 57 · Advanced · Version: 3
- C++ · Lesson 58 · Advanced · Version: 3
- C++ · Lesson 59 · Advanced · Version: 3
Sources
- C++ working draft — Class template vector [vector]
- C++ working draft — Container requirements [container.reqmts]
- C++ working draft — Sequence containers [sequence.reqmts]
- C++ working draft — Vector capacity [vector.capacity]
- C++ working draft — Vector data [vector.data]
- C++ working draft — Vector modifiers [vector.modifiers]
- C++ working draft — Specialization of vector for bool [vector.bool]
- C++ working draft — Range access [iterator.range]