2D Vector in C++

2D Vector (Vector of Vectors) in C++.

IntermediateSTL Containers ProgramsExample 3 of 10
2d-vector.cpp
1#include <iostream>
2#include <vector>
3using namespace std;
4
5int main() {
6 // Create 2D vector
7 vector<vector<int>> matrix;
8
9 // Initialize with 3 rows and 4 columns
10 int rows = 3, cols = 4;
11 matrix.resize(rows, vector<int>(cols));
12
13 // Fill matrix
14 int value = 1;
15 for (int i = 0; i < rows; i++) {
16 for (int j = 0; j < cols; j++) {
17 matrix[i][j] = value++;
18 }
19 }
20
21 // Display matrix
22 cout << "2D Vector (Matrix):" << endl;
23 for (int i = 0; i < rows; i++) {
24 for (int j = 0; j < cols; j++) {
25 cout << matrix[i][j] << "\t";
26 }
27 cout << endl;
28 }
29
30 // Another way: Initialize with values
31 vector<vector<int>> matrix2 = {
32 {1, 2, 3},
33 {4, 5, 6},
34 {7, 8, 9}
35 };
36
37 cout << "\nMatrix 2:" << endl;
38 for (const auto& row : matrix2) {
39 for (int num : row) {
40 cout << num << " ";
41 }
42 cout << endl;
43 }
44
45 // Access elements
46 cout << "\nElement at [1][2]: " << matrix2[1][2] << endl;
47 cout << "Number of rows: " << matrix2.size() << endl;
48 cout << "Number of columns in first row: " << matrix2[0].size() << endl;
49
50 return 0;
51}

Output

2D Vector (Matrix):
1	2	3	4	
5	6	7	8	
9	10	11	12	

Matrix 2:
1 2 3
4 5 6
7 8 9

Element at [1][2]: 6
Number of rows: 3
Number of columns in first row: 3

What's going on

The program demonstrates 2D vector operations:

Creating 2D vector (vector of vectors)
Setting dimensions using resize()
Accessing elements using matrix[i][j]
Initializing with values
Working with different row sizes

2D vectors provide flexible, dynamic two-dimensional data structures.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <vector>
Provides vector container class.

Understanding 2D Vectors

2D Vector Concept

:

Vector where each element is a vector
Creates two-dimensional structure
Each row is a separate vector
Can have different row sizes (jagged arrays)

Declaration

:

vector<vector<int>> matrix;

How it works

:

Outer vector contains rows
Each row is a vector of elements
Access: matrix[row][column]

Creating 2D Vector

Empty 2D Vector

:

vector<vector<int>> matrix;

Setting Dimensions

:

matrix.resize(rows, vector<int>(cols));

How it works

:

resize() sets number of rows
Each row initialized with cols columns
Creates rows × cols matrix
All elements default-initialized

Accessing Elements

Element Access

:

matrix[i][j] // Element at row i, column j

How it works

:

First index [i]: row number
Second index [j]: column number
Like 2D arrays but dynamic
Can access and modify

Example

:

matrix[1][2] = 6; // Set element at row 1, column 2

Filling 2D Vector

Nested Loops

:

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

matrix[i][j] = value++;

}

}

How it works

:

Outer loop: iterate rows
Inner loop: iterate columns
Fill each element sequentially
Can fill with any pattern

Initializing with Values

Initialization

:

vector<vector<int>> matrix2 = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

How it works

:

Initialize with nested braces
Each inner brace is a row
Rows can have different sizes
Creates jagged array if needed

Different Row Sizes

Jagged Array

:

vector<vector<int>> jagged = {

{1, 2},

{3, 4, 5},

{6, 7, 8, 9}

};

How it works

:

Each row can have different size
More flexible than 2D arrays
Useful for irregular data
Access: jagged[i][j] (j must be < row size)

When to Use 2D Vectors

Best For

:

Matrices and grids
Tables with variable row sizes
Dynamic 2D data structures
When size unknown at compile time
Jagged arrays

Example Scenarios

:

Matrix operations
Game boards
Data tables
Graph representations
Image processing

Important Considerations

Memory Layout

:

Rows stored separately
Not contiguous like 2D arrays
Each row can be in different memory location
Slightly less cache-friendly

Performance

:

Access: O(1) like arrays
Resizing: can be expensive
More flexible than 2D arrays
Slight overhead vs 2D arrays

Size Information

:

matrix.size(): number of rows
matrix[i].size(): columns in row i
Can have different row sizes

return 0;

This ends the program successfully.