In C ++ programming, I encountered the feature of allocating / freeing memory when creating dynamic arrays, and to avoid confusion, I decided to use std::vector . Googled information about what methods are there, but could not find any reasonable information about working with two-dimensional vectors (it is not strong in OOP). Can someone after him explain the general logic of working with two-dimensional vectors in principle?

Closed due to the fact that the issue is too general for participants Harry , Vlad from Moscow , αλεχολυτ , user194374, Denis Bubnov 22 Feb '17 at 6:19 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

     #include <vector> #include <iostream> using namespace std; int main() { vector <vector<int> > v; // создаем вектор векторов int n; cin >> n; v.resize(n+1); // после resize в векторе появилось n пустых векторов, в которые ты можешь push_back-кать // к ячейкам вектора можешь обращаться по такому циклу for(int i = 0; i < (int)v[i].size(); ++i) return 0; } 
    • Thank! I'll try to figure it out. And if in advance it is not known what dimension the vector is? (2, 3 and t d, te n-dimensional? Can you tell me how to get out of the situation? - Andrei Samotarov
    • @ AndreiSamotarov every time when we increase the dimension of a vector, we do resize as much as we need - Alexey
    • @ Aleksey What is the point in reducing the type of the value returned by the size () function to the int type? And why call push_back if the required number of elements in the vector is already set using the resize function? - Vlad from Moscow
    • @VladfromMoscow Personally, my compiler swears at it, says that there are different types, and the question with push_back did not understand a bit - Alexey
    • @Alexey Correctly does the compiler, that swears, because it is not clear why the type is Int. As for the second remark, then why use the push_back method, when you already using resize have already selected the required number of elements in the vector? - Vlad from Moscow