Vector of Vectors in C++ STL with Examples (original) (raw)
Vector of Vectors is a two-dimensional (2D) vector with a variable number of rows, where each row is a vector.
- Each index of a vector stores a vector that can be traversed and accessed using iterators.
- Each inner vector represents a row
- The outer vector represents the collection of rows
**Syntax:
C++ `
vector<vector> vec;
`
where **vec is the name of a 2D vector, and **data_type is the type of elements.
Initialization
In the most of the cases, the first operation of a vector of vectors is initialized with a few elements, either by providing default values with a specified size or using an initialization list.
C++ `
#include #include 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;}
`
To more about initialization of vector of vectors, refer to the article – Initialize 2D Vector
Accessing and Updating Elements
Arrays are accessed using their indexes, while a vector of vectors represents as matrix requires both row and column values as indexes to access or update any element. We can use either the [] operator or the at() function to access and update elements in a vector of vectors.
C++ `
#include #include using namespace std;
int main() { vector<vector> v = { {1, 2, 3}, {4, 5, 6} };
// Insert a new row
v.push_back({7, 8, 9});
// Insert element in 2nd row
v[1].insert(v[1].begin() + 1, 4);
// Access element
cout << v[0][0] << endl;
// Update element
v[1][0] = 5;
// Access using at()
cout << v.at(1).at(0);
return 0;}
`
Traversing
To traverse a 2D vector, you need to loopthrough each row and column using nested loops, and then access the elements by their respective indices.
C++ `
#include #include using namespace std;
int main() { vector<vector> v = { {1, 2, 3}, {4, 5, 6}, {7, 8, 6} };
for (auto& row : v) {
for (int val : row) {
cout << val << " ";
}
cout << endl;
}
return 0;}
`
Traverse a vector in multiple ways - Iterator 2D vector
**Note: Using & with auto (e.g., auto&) creates a reference to the original element rather than a copy. This means no new memory is allocated for the data, and any modifications made will directly affect the original container. It is efficient and preferred when you want to avoid copying or need to modify elements.
Index-Based Traversal of 2D Vector
C++ `
#include #include using namespace std;
int main() { vector<vector> 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;}
`
Insertion
In a vector of vectors, values can be added as a new row or inserted into an existing row. You can insert values at any position using the insert() function, or add them at the end using push_back().
C++ `
#include #include using namespace std;
int main() { vector<vector> 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
Different ways to insert elements - Insert Elements into vector of vectors
Removal or Deletion
In a 2D vector, deletion can be performed in two ways:
- Removing a row
- Deleting a specific value within a row
To delete elements, you can use the erase() function for removing an element at a particular position or range. Alternatively, the pop_back() function can be used to remove the last element or row of the vector. Here’s an example illustrating how these operations work:
C++ `
#include #include using namespace std;
int main() { vector<vector> 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;}
`