A vector of vectors is a two-dimensional dynamic array in C++ where each element of the outer vector is itself a vector. Unlike traditional 2D arrays, a vector of vectors can have a variable number of rows and columns.
- The outer vector stores the rows.
- Each inner vector represents a row of elements.
- The number of rows and columns can grow or shrink dynamically.
vector<vector<int>> matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Syntax
vector<vector<data_type>> vec;
where:
- data_type is the type of elements.
- vec is the name of the 2D vector.
Initializing a Vector of Vectors
A vector of vectors can be initialized using a fixed size with default values or by using an initializer list.
Using Fixed Size and Default Value
vector<vector<int>> v(3, vector<int>(3, 7));
This creates a 3 × 3 matrix with all values initialized to 7.
Using Initializer List
vector<vector<int>> v = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Example
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Fixed size with default value
vector<vector<int>> v1(3, vector<int>(3, 7));
// Using initializer list
vector<vector<int>> v2 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
return 0;
}
Accessing Elements
Elements of a vector of vectors are accessed using two indices: one for the row and one for the column.
v[row][column]
We can also use the at() function.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int>> v = {
{1, 2, 3},
{4, 5, 6}
};
cout << v[0][0] << endl;
cout << v.at(1).at(2);
return 0;
}
Output
1 6
Updating Elements
Elements can be modified by assigning a new value.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int>> v = {
{1, 2, 3},
{4, 5, 6}
};
v[1][0] = 10;
cout << v[1][0];
return 0;
}
Output
10
Traversing a Vector of Vectors
A vector of vectors can be traversed using nested loops.
Using Range-Based Loop
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int>> v = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (auto& row : v) {
for (int val : row) {
cout << val << " ";
}
cout << endl;
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9
Note: Using auto& creates a reference instead of making a copy, which improves performance and allows modification of elements.
Using Index-Based Traversal
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int>> v = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Traversing using indices
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].size(); j++) {
cout << v[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9
Inserting Elements
In a vector of vectors, insertion can be performed in two ways:
- Adding a new row.
- Inserting elements into an existing row.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int>> v = {
{1, 2, 3},
{4, 5, 6}
};
// Add new row
v.push_back({7, 8, 9});
// Insert element in row
v[1].insert(v[1].begin() + 1, 4);
for (auto& row : v) {
for (int val : row) {
cout << val << " ";
}
cout << endl;
}
return 0;
}
Output
1 2 3 4 4 5 6 7 8 9
Removing Elements
Elements can be removed by:
- Removing an entire row.
- Removing specific elements within a row.
Example
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int>> v = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 6}
};
// Delete first row
v.erase(v.begin());
// Delete element in row
v[0].erase(v[0].begin() + 1);
// Delete last row
v.pop_back();
for (auto& row : v) {
for (int val : row) {
cout << val << " ";
}
cout << endl;
}
return 0;
}
Output
4 6
Advantages of Vector of Vectors
A vector of vectors provides several benefits over traditional two-dimensional arrays:
- Dynamic number of rows and columns.
- Automatic memory management.
- Supports STL algorithms and iterators.
- Easy insertion and deletion of rows.
Disadvantages of Vector of Vectors
Despite its flexibility, a vector of vectors has some limitations:
- Memory is not stored contiguously like arrays.
- Slightly slower than fixed-size 2D arrays.
- Additional memory overhead due to nested vectors.