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.
Try one of our 300+ courses and learning paths: C++ for Programmers.
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 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 a const_iterator pointing to the first element in the container
Note: 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 a const_iterator pointing to the past-the-end element in the containercrbegin: returns a const_reverse_iterator pointing to the last element in the containercrend: returns a const_reverse_iterator pointing 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 vector
Note: 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 vector
Note: 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
Try one of our 300+ courses and learning paths: C++ for Programmers.
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 (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 than size() (often doubling) to reduce the cost of reallocations.
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.
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.
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.
| 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 |
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.
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++:
This method passes an array to the constructor of the vector class. The passed array contains the elements which will populate the vector.
#include <iostream>#include <vector>using namespace std;int main() {vector<int> vec{1,2,3,4,5};for (int i = 0; i < vec.size(); i++){cout << vec[i] << " ";}return 0;}
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.
#include <iostream>#include <vector>using namespace std;int main() {vector<int> vec;vec.push_back(1);vec.push_back(2);vec.push_back(3);vec.push_back(4);vec.push_back(5);for (int i = 0; i < vec.size(); i++){cout << vec[i] << " ";}return 0;}
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).
#include <iostream>#include <vector>using namespace std;int main() {int num_of_ele = 10;// the overloaded constructor takes the number of// values and the value itself as parameters.vector<int> vec(num_of_ele, 1);for (int i = 0; i < vec.size(); i++){cout << vec[i] << " ";}return 0;}
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.
#include <iostream>#include <vector>using namespace std;int main() {vector<int> vec_1{1,2,3,4,5};vector<int> vec_2(vec_1.begin(), vec_1.end());for (int i = 0; i < vec_2.size(); i++){cout << vec_2[i] << " ";}return 0;}
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.
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.
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.
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.
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());
Enables direct pointer access for performance-critical code.
Useful for memory-mapped I/O, GPU buffers, or SIMD operations.
Makes std::vector ideal 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.
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:
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!