C++ 2D Vectors
What Will You Learn in This Guide?
This guide teaches you how to create 2D Vectors using the C++ Standard Template Library (STL).
Unlike arrays, 2D vectors have dynamic dimensions, so data can be added, deleted, and resized at runtime.
It is ideal for high-performance data management in matrix, table or graph structures.
2D Vector Concept
A 2D vector is actually a vector of vectors.
Each row (vector<int>) is a vector on its own, and the outer vector contains them:
vector<vector<int>> tablo {{1, 2, 3}, {4, 5}, {6, 7, 8}};
- Note: Lines can be of different lengths; This structure is called “jagged array”.
1. Required Libraries
To use vectors in C++, include this header file:
#include <vector>
Tüm STL kütüphanelerini tek seferde dahil etmek isterseniz:
#include <bits/stdc++.h>
2. Creating and Initializing 2D Vector
1. Static Initialization (While Values Are Known)
If you know your values beforehand, you can initialize it directly:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int>> matris {{10, 20, 30}, {40, 50}, {60, 70, 80}};
for (int i = 0; i < matris.size(); i++) {
for (int j = 0; j < matris[i].size(); j++)
cout << matris[i][j] << " ";
cout << endl;
}
}
Çıktı:
10 20 30
40 50
60 70 80
2. Starting by Specifying a Size
To know the number of rows/columns and initialize all elements with the same value:
int satir = 4, sutun = 3;
vector<vector<int>> tablo(satir, vector<int>(sutun, 0));
Çıktı:
0 0 0
0 0 0
0 0 0
0 0 0
vector<int>(sutun, 0)→ creates 3 columns for each row, assigns 0 to all cells. table(row, …) → repeats this structure as 4 rows.
3. Blank 2D Vector
If you want to add data dynamically you can create empty:
vector<vector<int>> tablo;
3. Navigating 2D Vector
With For Loop
for (int i = 0; i < tablo.size(); i++) {
for (int j = 0; j < tablo[i].size(); j++)
cout << tablo[i][j] << " ";
cout << endl;
}
With Iterator:
vector<vector<int>> tablo {{1, 0, 1}, {0, 1}, {1, 0, 1}};
vector<vector<int>>::iterator itSatir;
vector<int>::iterator itSutun;
for (itSatir = tablo.begin(); itSatir != tablo.end(); itSatir++) {
for (itSutun = itSatir->begin(); itSutun != itSatir->end(); itSutun++)
cout << *itSutun << " ";
cout << endl;
}
- begin() → starting iterator end() → iterator pointing after
4. Adding Elements to a 2D Vector
push_back() – Add New Line
vector<vector<int>> v;
v.push_back({10, 0, 10});
v.push_back({0, 5});
v.push_back({7, 8, 9});
Çıktı:
10 0 10
0 5
7 8 9
insert() – Insert at Specific Position
auto it = v.begin();
v.insert(it + 1, {1, 2, 3}); // 2. satıra ekler
Çıktı:
10 0 10
1 2 3
0 5
7 8 9
- The insert() function takes an iterator, not an index.
5. Delete Element
pop_back() – Delete Last Line
v.pop_back();
erase() – Delete Specific Row
v.erase(v.begin() + 1);
clear() – Clear All
v.clear();
Technical Specifications Table
| Feature | Description |
|---|---|
| Data Structure | vector<vector<T>> (nested dynamic structure) |
| Default Initialization | vector<vector<int>>(satir, vector<int>(sutun, 0)) |
| Adding Functions | push_back(), insert() |
| Delete Functions | pop_back(), erase(), clear() |
| Roaming | Nested loop for or iterator |
| Time Complexity | O(N × M) (row × column) |
| Scope | Matrix, graph, table data structures |
Frequently Asked Questions (FAQ)
- Why use 2D Vector instead of 2D Array?
Because vectors are dynamic, arrays are fixed size. You can change the size, add and delete data while the program is running.
- Can lines be of different lengths?
Yes. Each interior vector is independent and can have a different number of columns.
- Can I add a single number to a 2D vector?
No, it can only be added to the subvector (v[i].push_back(x)).
- How do I learn the capacity of 2D vectors?
v.size() → number of rows, v[i].size() → number of columns.
- Does clear() completely clear memory?
No, it preserves the capacity but removes the elements.
Result
It is one of the most flexible solutions for working with multidimensional data such as 2D vectors, matrix, graph and tabular structures in C++. It is much more dynamic and secure than fixed size arrays.
You can try it immediately on the GenixNode platform to test this structure in your own applications.

