Auto iterator for 2d vector

  2d-vector, c++, vector

How can I access 2d vector using auto iterator?

I write a code using auto iterator but it is not run just showing: –

Compilation Error:

Compilation Error prog.cpp: In member function
std::vectorstd::vector<int > Solution::sortedMatrix(int,
std::vectorstd::vector<int >): prog.cpp:18:31: error: no match for
operator[] (operand types are std::vectorstd::vector<int > and
__gnu_cxx::__normal_iteratorstd::vector<int*, std::vectorstd::vector<int > >)
for(auto col = Mat[row].begin(); col != Mat[row].end(); col++){

Code: –

vector<vector<int>> sortedMatrix(int N, vector<vector<int>> Mat) {

    int store[N*N];

    int k = 0;

    for(auto row = Mat.begin(); row != Mat.end(); row++){
        for(auto col = Mat[row].begin(); col != Mat[row].end(); col++){
            store[k++] = Mat[row][col];
        }
    }
    
    sort(store, store+k);
    k = 0;
    
    for(auto row = Mat.begin(); row != Mat.end(); row++){
        for(auto col = row->begin(); col != row->end(); col++){
             Mat[row][col] = store[k++] ;
        }
    }
    return Mat;
}

Even when I use instead of that code
for(int row = 0; row < N; row++){
for(int col = 0; col < N; col++)

It actually works.
But how I do that using auto iterator?

Problem Link

Source: Windows Questions C++

LEAVE A COMMENT