There is a certain piece of code that creates a two-dimensional field m [x] [y] and clogs it with zeros:

std::vector<std::vector<int> > vector_map; vector_map.push_back(std::vector<int>() ); vector_map.resize(count_h); for (unsigned int y = 0; y < count_h; y++) { for (unsigned int x = 0; x < count_w; x++) { vector_map[x].push_back(0); } } 

Initialization of count_w and count_h occurs at the moment of calling the function in which the given code is contained. AND...

  1. How to withdraw the whole thing? It is desirable to implement this using the same two cycles as above.
  2. How to get access to an arbitrary field of the resulting matrix and, if necessary, change it?

I was able to clog values, as well as their conclusion:

 std::vector<int> vector_map; vector_map.resize(count_h * count_w); for (unsigned int y = 0; y < count_h; y++) { vector_map.push_back(y); for (unsigned int x = 0; x < count_w; x++) { vector_map.push_back(x); } } for (unsigned int y = 0; y < count_h; y++) { for (unsigned int x = 0; x < count_w; x++) { std::cout << vector_map[x*y]; } std::cout << std::endl; } 

The result was something like this:

 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 

But how to get access and change any arbitrary element - I can't imagine the mind. I hope for your help.

Thank you in advance.

    1 answer 1

    Actually, everything turned out to be much simpler. Interface:

     std::vector<std::vector<int> > vct_map; 

    Implementation in function:

     vector<vector <int> > map(count_w, vector<int>(count_h));; vct_map.resize(count_w * count_h); for (int y = 0; y < count_h; y++) { for (int x = 0; x < count_w; x++) { map[x][y] = 1; } } vct_map = map; 

    The sizes of the local vector in the function are set in advance by unknown values, transmitted through the function call, processed as I need and then simply assigned to the class member. Maybe not the perfect solution, but very short and understandable. Completely solves my problem with access to arbitrary data and reading from the result obtained.