A vector is a dynamic array — unlike regular arrays, it can grow or shrink automatically as elements are added or removed. It is the most commonly used STL container and offers the performance of arrays combined with the convenience of automatic memory management.
Including and Declaring a Vector
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums; // empty vector of ints
vector<string> names; // empty vector of strings
vector<double> prices(5); // vector with 5 zeros
vector<int> scores(4, 100); // vector with 4 elements, all 100
return 0;
}
Adding Elements
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
Accessing Elements
cout << v[0] << endl; // 10 (no bounds check)
cout << v.at(1) << endl; // 20 (bounds checked — safer)
cout << v.front()<< endl; // 10 (first element)
cout << v.back() << endl; // 30 (last element)
Iterating Over a Vector
vector<int> v = {10, 20, 30, 40, 50};
// Range-based for loop
for (int x : v) {
cout << x << " ";
}
cout << endl;
Output:
10 20 30 40 50
Common Vector Functions
| Function | Description |
|---|
push_back(val) | Add element at the end |
pop_back() | Remove the last element |
size() | Number of elements |
empty() | Returns true if empty |
clear() | Remove all elements |
insert(pos, val) | Insert at position |
erase(pos) | Remove element at position |
resize(n) | Change size to n |
capacity() | Allocated memory capacity |
begin() / end() | Iterators to start/end |
Full Example — Student Scores
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int main() {
vector<int> scores = {78, 92, 55, 88, 70};
// Sort
sort(scores.begin(), scores.end());
cout << "Sorted: ";
for (int s : scores) cout << s << " ";
cout << endl;
// Min and Max
cout << "Min: " << *min_element(scores.begin(), scores.end()) << endl;
cout << "Max: " << *max_element(scores.begin(), scores.end()) << endl;
// Sum
int total = accumulate(scores.begin(), scores.end(), 0);
cout << "Total: " << total << endl;
cout << "Average: " << total / (double)scores.size() << endl;
return 0;
}
Output:
Sorted: 55 70 78 88 92
Min: 55
Max: 92
Total: 383
Average: 76.6
Inserting and Erasing
vector<int> v = {1, 2, 4, 5};
v.insert(v.begin() + 2, 3); // insert 3 at index 2
for (int x : v) cout << x << " ";
cout << endl; // 1 2 3 4 5
v.erase(v.begin() + 1); // remove element at index 1
for (int x : v) cout << x << " ";
cout << endl; // 1 3 4 5
2D Vector — Vector of Vectors
vector<vector<int>> matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (auto &row : matrix) {
for (int val : row) {
cout << val << " ";
}
cout << endl;
}
Output:
1 2 3
4 5 6
7 8 9
Vector vs Array
| Feature | Array | Vector |
|---|
| Size | Fixed at compile time | Dynamic — grows/shrinks |
| Memory | Stack | Heap |
| Functions | None built-in | Many built-in (push_back, etc.) |
| Safety | No bounds checking | .at() throws on out-of-bounds |
Key Takeaways
- Vectors are dynamic arrays that resize automatically.
push_back() adds elements; pop_back() removes the last one.- Use
.at() for safe access with bounds checking. - STL algorithms like
sort() and find() work directly with vectors. - 2D vectors (vector of vectors) replace 2D arrays with more flexibility.