Vector of Vectors in C++
A versatile data structure in C++ called a vector of vectors offers a dynamic and adaptable method of storing and manipulating two-dimensional arrays. In contrast to conventional arrays, vectors of vectors are implemented using the std::vector container class, which provides effective memory management and dynamic scaling. The outer vector's constituent elements are all vectors in and of themselves, forming a structure that resembles a matrix. This configuration makes it simple to work with rows and columns and enables dynamic resizing of dimensions. Dynamic resizing comes in handy when the size of the matrix needs to be adjusted while the program is running or is unknown at compile time. Vectors of vectors are frequently used to represent grids, tables, and sparse matrices, among other situations that call for a dynamic two-dimensional data structure. Vector implementation and manipulation are made easy and productive by the robust utilities offered by the Standard Template Library (STL) in C++.
Methods used are:
Initialization and Declaration:
- Declare the following vector of vectors: matrix std::vector<std::vector<T>>;
- Create an initial vector of vectors with the following size: matrix(rows, std::vector<T>(cols));
Accessing Elements:
- Use the following to access a matrix element: value = matrix[row][col];
- Change a matrix element as follows: matrix[row][col] = new_value;
Sizing out:
- Row size can be adjusted using matrix.resize(new_rows);
- Row-specific column resizing can be achieved by using matrix[row].resize(new_cols);
Iterating through the Matrix:
for (size_t i = 0; i < matrix.size(); ++i) {
for (size_t j = 0; j < matrix[i].size(); ++j) {
// Access or manipulate matrix[i][j]
}
}
Including and Eliminating Rows:
- A new row is added to the matrix: grid.push_back(cols) in std::vector<T>;
- Take out a row using the formula: matrix.back_pop();
Clearing Out the Matrix:
- Use matrix.clear() to remove every entry from the matrix;
Example:
Let's take an example to illustrate the use of vector of vectors in C++.
#include <iostream>
#include <vector>
int main() {
// Declare and initialize a vector of vectors to represent a 2D matrix
std::vector<std::vector<int>> matrix;
// Initialize the matrix with 3 rows and 4 columns, setting all elements to 0
size_t rows = 3;
size_t cols = 4;
matrix.resize(rows, std::vector<int>(cols, 0));
// Print the initial matrix
std::cout << "Initial Matrix:" << std::endl;
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
// Modify an element in the matrix
matrix[1][2] = 15 ;
// Print the modified matrix
std::cout << "\nModified Matrix:" << std::endl;
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
// Resize the matrix to add a new row
matrix.push_back(std::vector<int>(cols, 1));
// Print the matrix after adding a new row
std::cout << "\nMatrix After Adding a New Row:" << std::endl;
for (size_t i = 0; i < matrix.size(); ++i) {
for (size_t j = 0; j < matrix[i].size(); ++j) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
Output:
Initial Matrix:
0 0 0 0
0 0 0 0
0 0 0 0
Modified Matrix:
0 0 0 0
0 0 15 0
0 0 0 0
Matrix After Adding a New Row:
0 0 0 0
0 0 15 0
0 0 0 0
1 1 1 1
Example 2:
Let's take another example to illustrate the use of vector of vectors in C++.
#include <iostream>
#include <vector>
int main() {
// Declare a vector of vectors to represent a 2D matrix
std::vector<std::vector<int>> matrix;
// Get the number of rows and columns from the user
size_t rows, cols;
std::cout << "Enter the number of rows: ";
std::cin >> rows;
std::cout << "Enter the number of columns: ";
std::cin >> cols;
// Initialize the matrix with user-specified dimensions
matrix.resize(rows, std::vector<int>(cols, 0));
// Get matrix elements from the user
std::cout << "Enter the matrix elements:" << std::endl;
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
std::cout << "Element at position [" << i << "][" << j << "]: ";
std::cin >> matrix[i][j];
}
}
// Print the user-defined matrix
std::cout << "\nUser-defined Matrix:" << std::endl;
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
Output:
Enter the number of rows: 4
Enter the number of columns: 4
Enter the matrix elements:
Element at position [0][0]: 1
Element at position [0][1]: 2
Element at position [0][2]: 4
Element at position [0][3]: 5
Element at position [1][0]: 6
Element at position [1][1]: 23
Element at position [1][2]: 51
Element at position [1][3]: 56
Element at position [2][0]: 45
Element at position [2][1]: 12
Element at position [2][2]: 88
Element at position [2][3]: 16
Element at position [3][0]: 45
Element at position [3][1]: 32
Element at position [3][2]: 15
Element at position [3][3]: 75
User-defined Matrix:
1 2 4 5
6 23 51 56
45 12 88 16
45 32 15 75
Conclusion:
In C++, handling two-dimensional data structures is made dynamic and flexible with the help of vectors of vectors. Resizable matrices are made possible by the std::vector container class, which provides flexibility in terms of both row and column counts. This dynamic scaling is especially helpful in situations when the matrix's size needs to change while the program is running or is unknown at compile time. Numerous applications benefit from this data structure, including handling sparse matrices and displaying grids and tables. The C++ Standard Template Library (STL) expands on the capabilities of vectors by providing a variety of easy-to-use manipulation techniques.