What is Vector in C++? Get started in 5 minutes
C++ vector is a template class in the Standard Template Library (STL) that functions as a more refined array. Unlike arrays, vectors can resize themselves automatically when you insert or delete elements, so they’re great to use when working with constantly changing data. Using vectors in your C++ program enables you to store your data with more flexibility and efficiency. Today, we’ll dive deeper into C++ vectors and discuss benefits, functions, and initialization.
Get hands-on with C++ today.#
Try one of our 300+ courses and learning paths: C++ for Programmers.
What is std::vector in C++?#
C++ vectors (also known as std::vector) are sequence containers that represent arrays that can change in size. They’re used to work with dynamic data, and they use contiguous storage locations for their elements. You can efficiently access the elements using offsets on regular pointers. Internally, C++ vectors use dynamically allocated arrays to store their elements. The array may require reallocation so it can grow in size when new elements are inserted.
Vector containers may allocate additional storage space to accommodate for potential growth, so the container may have a larger capacity than what’s actually being stored inside of it. You can use libraries to apply different growth strategies to strike a balance between reallocations and memory usage, but reallocations should only happen at logarithmically growing intervals of size. This allows you to use amortized constant time complexity when inserting individual elements at the end of the vector.
Since C++ vectors perform automatic reallocation, they’re able to manage storage and grow dynamically in an efficient way. Although they consume more memory than arrays, their efficiency and flexibility make using vectors worthwhile.
Let’s discuss a few more benefits of C++ vector:
- There’s no maximum size, which is helpful if you don’t know how big your data will be beforehand
- Their resizing capabilities make it easier to handle dynamic elements
- Since C++ vector is a template class, you don’t have to type in the same code to handle different data
- Whenever the total amount of memory is used, automatic reallocation happens
- You can easily copy and assign other vectors
C++ vector functions#
There are many public member functions associated with C++ vectors. Let’s take a look at some of the functions and what they do.
cbegin: returns aconst_iteratorpointing to the first element in the containerNote: A
const_iteratoris an iterator that points to the const content. It can be increased and decreased like theiteratorreturned byvector::begin, but it can’t be used to modify the contents it points to.cend: returns aconst_iteratorpointing to the past-the-end element in the containercrbegin: returns aconst_reverse_iteratorpointing to the last element in the containercrend: returns aconst_reverse_iteratorpointing to the theoretical element preceding the first element in the containeremplace_back: inserts a new element at the end of the vector, right after the current last elementmax_size: returns the maximum sizepop_back: removes the last element in the vectorpush_back: adds a new element at end of the vectorrbegin: returns a reverse iterator pointing to the last element in the vectorNote: Reverse iterators iterate backward. Increasing a reverse iterator moves it towards the beginning of the container.
rend: returns a reverse iterator pointing to the theoretical element preceding the first element in the vectorshrink_to_fit: requests the container to reduce its capacity to fit its sizestd::vector::end: returns an iterator referring to the past-the-end element in the vector containervector::capacity: returns the size of the storage space currently allocated for the vectorvector::front: returns a reference to the first element in the vectorvector::back: returns the last element of the vectorvector::size: returns the number of elements in the vectorNote: This returns the number of actual objects held in the vector, which isn’t necessarily equal to its storage capacity.
vector::begin: returns an iterator pointing to the first element in the vector
Get hands-on with C++ today.#
Try one of our 300+ courses and learning paths: C++ for Programmers.
Understanding Size, Capacity, and Memory Management#
A std::vector automatically resizes as elements are added — but understanding how it manages memory helps you write faster, safer, and more predictable code.
Vectors balance flexibility with performance by allocating memory strategically, and knowing how this works can help you avoid common pitfalls.
Size vs Capacity#
Size (
size()) – The number of elements currently in the vector.
Changes when you add or remove elements.Capacity (
capacity()) – The total memory currently allocated for the vector.
Typically grows faster thansize()(often doubling) to reduce the cost of reallocations.
Growth Behavior#
When a vector runs out of space, it allocates a larger block of memory and copies or moves existing elements into it.
This process — known as reallocation — can be expensive, so minimizing it is key to performance.
Reserve and Shrink#
You can control vector memory growth to improve efficiency:
std::vector<int> v;v.reserve(1000); // Pre-allocate space for 1000 elements
reserve(n): Pre-allocates memory to avoid repeated reallocations.shrink_to_fit(): Requests memory reduction when the vector’s size is smaller than its capacity (non-binding — the compiler may ignore it).
Note: Capacity never decreases automatically unless explicitly shrunk.
Iterator Invalidation: A Common Beginner Mistake#
When a vector resizes or changes content, its internal memory address may change.
This means pointers, references, or iterators to its elements can become invalid — leading to undefined behavior.
Common Operations and Iterator Validity#
| Invalidates if reallocation occurs |
| Invalidates all iterators after the insertion point |
| Invalidates iterators from the erased element onward |
| Invalidates all iterators if capacity changes |
| Invalidates all iterators |
Best Practices#
Refresh iterators, pointers, or references after any operation that may reallocate.
Use
reserve()to minimize reallocations.When erasing elements, iterate backwards to avoid skipping items.
How to initialize a C++ vector#
Now that we know more about the basics and some of the functions of C++ vectors, let’s discuss the initialization methods.
There are four ways to initialize a vector in C++:
1. Using an array#
This method passes an array to the constructor of the vector class. The passed array contains the elements which will populate the vector.
2. Pushing the values one at a time#
All the elements that need to populate a vector can be pushed, one at a time, into the vector using the vector class method push_back.
3. Using the overloaded constructor of the vector class#
This method is mainly used when a vector needs to be populated with multiple elements of the same value (if a vector needs to be populated with ten 1’s, for example).
4. Using an already initialized vector#
This method passes the begin() and end() iterators of an already initialized vector to a vector class constructor. A new vector is initialized and populated by the elements in the old vector.
Using emplace_back() for Efficiency#
While push_back() copies or moves an object, emplace_back() constructs it in place directly within the vector’s allocated memory — reducing overhead.
std::vector<std::pair<int, std::string>> v;v.emplace_back(1, "hello"); // Constructs the pair directly in-place
emplace_back() is particularly useful for large objects, classes without trivial copy constructors, or cases where temporary object creation would otherwise add cost.
Working with Ranges and Modern C++ Features#
Modern C++ introduces range-based utilities that make working with vectors simpler and safer.
std::erase/std::erase_if– Simplify element removal.Range constructors – Create vectors directly from other containers or iterators.
std::ranges::to– Convert ranges or views into vectors cleanly.
std::vector<int> nums = {1, 2, 3, 4, 5};std::erase_if(nums, [](int n) { return n % 2 == 0; }); // Removes even numbers
These features improve readability, maintainability, and safety, making them a must-know for modern C++ developers.
vector<bool>: A Special Case#
std::vector<bool> is a partial specialization that packs boolean values as bits to save memory.
However, this optimization changes its behavior in subtle ways.
Drawbacks#
Slower operations due to bit-packing/unpacking.
Element references are proxy objects, not real
bool&.Incompatible with some standard algorithms expecting real references.
If you need normal semantics, use:
std::vector<char> // orstd::vector<uint8_t>
These provide standard behavior at the cost of slightly more memory.
Contiguity and Interoperability with C APIs#
Since C++17, std::vector guarantees contiguous storage — meaning you can safely access its internal array for interoperability with C or low-level APIs.
std::vector<int> nums = {1, 2, 3, 4};some_c_function(nums.data(), nums.size());
Why It Matters#
Enables direct pointer access for performance-critical code.
Useful for memory-mapped I/O, GPU buffers, or SIMD operations.
Makes
std::vectorideal for systems requiring high performance and memory locality.
By mastering size, capacity, and memory behavior, you’ll write efficient, robust, and modern C++ code that scales from simple apps to high-performance systems.
What to learn next#
Using C++ vectors in your program will allow you to store your data with more flexibility and efficiency. They’re an ideal tool to use when working with constantly changing data. Now that you know more about vectors, you’re ready to move into more C++ concepts.
Some recommended concepts to cover next are:
- C++ templates
- Concurrency with modern C++
- Embedded programming in C++
To learn to code in C++, get started with Educative’s hands-on learning path C++ for Programmers. In this learning path, you’ll dive deeper into C++ fundamentals, working your way from basic to advanced concepts. By the end, you’ll have enough C++ skills to confidently solve real-world problems and impress your interviewers.
Happy learning!