Mastering C++ Vectors: Dynamic Arrays, Iterators, and Practical Examples
What is a C++ Vector?
A C++ vector is a dynamic array that automatically resizes itself as elements are added or removed. Its elements are stored contiguously, enabling fast indexed access and traversal via iterators.
Insertion occurs at the end in amortized constant time. Removing the last element is also constant time, while inserting or erasing at the beginning or middle requires shifting elements, resulting in linear time complexity.
When to Use a Vector?
- When you need a resizable container for elements that change over time.
- When the final size of the collection is unknown at compile‑time.
How to Initialize Vectors in C++
std::vector<data‑type> name (size, value); // size and value are optional
data‑typespecifies the element type.nameis the vector’s identifier.- The optional
sizeparameter reserves space;valueinitializes all elements.
Iterators
Iterators provide a pointer‑like interface to vector elements. Common iterator functions include:
begin()– first element.end()– past‑the‑end element.cbegin()– read‑only iterator to the first element.cend()– read‑only iterator past the last element.
Modifiers
Modifiers change the vector’s contents. Key functions are:
push_back()– append an element.insert()– insert at a specified position.pop_back()– remove the last element.erase()– delete a range of elements.clear()– remove all elements.
Example 1: Basic Iteration
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums;
for (int a = 1; a <= 5; ++a) {
nums.push_back(a);
}
std::cout << "Output from begin and end: ";
for (auto it = nums.begin(); it != nums.end(); ++it) {
std::cout << *it << ' ';
}
std::cout << "\nOutput from cbegin and cend: ";
for (auto it = nums.cbegin(); it != nums.cend(); ++it) {
std::cout << *it << ' ';
}
return 0;
}
Output:

Example 2: Advanced Modifiers
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums;
nums.assign(5, 1); // five elements, all initialized to 1
std::cout << "Vector contents: ";
for (size_t i = 0; i < nums.size(); ++i) {
std::cout << nums[i] << ' ';
}
nums.push_back(2);
std::cout << "\nLast element: " << nums.back();
nums.pop_back();
std::cout << "\nVector contents: ";
for (int a : nums) std::cout << a << ' ';
nums.insert(nums.begin(), 7);
std::cout << "\nFirst element: " << nums.front();
nums.clear();
std::cout << "\nSize after clear(): " << nums.size();
return 0;
}
Output:

Capacity Functions
Determine and manipulate a vector’s storage characteristics with the following functions:
size()– current number of elements.max_size()– theoretical maximum number of elements.capacity()– allocated storage capacity.resize(n)– change the number of elements ton, adding or removing as needed.empty()– true if the vector contains no elements.
Example 3: Capacity Exploration
#include <iostream>
#include <vector>
int main() {
std::vector<int> vector1;
for (int x = 1; x <= 10; ++x) {
vector1.push_back(x);
}
std::cout << "Vector size: " << vector1.size() << std::endl;
std::cout << "Vector capacity: " << vector1.capacity() << std::endl;
std::cout << "Maximum size of vector: " << vector1.max_size() << std::endl;
vector1.resize(5);
std::cout << "Vector size after resizing: " << vector1.size() << std::endl;
if (!vector1.empty())
std::cout << "Vector is not empty" << std::endl;
else
std::cout << "Vector is empty" << std::endl;
return 0;
}
Output:

Key Takeaways
- A C++ vector is a highly efficient, automatically resizable container.
- Elements reside in contiguous memory, providing fast random access.
- Insertion and removal at the back are amortized constant time; at the front or middle are linear time.
- Vectors are ideal for scenarios with unpredictable or changing data sizes.
C Language
- C++ For Loops Explained: Syntax, Workflow, and Practical Examples
- Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples
- Master C++ Dynamic Array Allocation: A Practical Guide with Code Examples
- Mastering std::stack in C++: A Comprehensive Guide with Practical Examples
- C++ Structs Explained with a Practical Example
- Mastering std::map in C++: Comprehensive Guide with Code Examples
- C++ Classes & Objects: A Practical Guide with Code Examples
- C++ Polymorphism Explained: Practical Examples & Key Concepts
- Mastering std::list in C++: Syntax, Functions & Practical Examples
- Dynamic Memory Allocation in C: Understanding malloc() with Practical Examples