Do not think of it as a multidimensional vector, think of it as a vector of vectors.
int n = 4; std::vector<std::vector<int>> vec(n, std::vector<int>(n));
I have included parentheses in (vec[i])[j] for understanding only.
Edit:
If you want to fill your vector with push_back , you can create a temporary vector in the inner loop, fill it, and then click on its vector:
for (int i = 0; i < n; i++) { std::vector<int> temp_vec; for (int j = 0; j < n; j++) { temp_vec.push_back(j); } vec.push_back(temp_vec); }
However, push_back calls result in slower code, since you do not need to constantly redistribute the vector, but you must also create a temporary one and copy it.
prazuber Dec 18 '12 at 15:59 2012-12-18 15:59
source share