0

here is my code

#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::vector;
int main() {

  vector<vector<int>> map{{0, 1, 0, 0, 0, 0},
                          {0, 1, 0, 0, 0, 0},
                          {0, 1, 0, 0, 0, 0},
                          {0, 1, 0, 0, 0, 0},
                          {0, 0, 0, 0, 1, 0}};
  for (int j : map) {
    for (int i :j) {
      cout << i ;
    }
    cout << "\n";
  }
}

Above code, there is an error in int j : map But when i change 'int' to 'auto', the code is working well

My question is what is proper type for 2d vector such as map? Why int is not working for 2d vector?

Kyuhwan Yeon
  • 151
  • 2
  • 8

1 Answers1

3

You must use like this. Because first for loop search in vector<vector<int>>. So j type must be vector<int> for first layer. My English is too bad so sorry i try the explain this. I hope this is helpfully.

for (vector<int> &j : map) {
    for (int i : j) {
      cout << i ;
    }
    cout << "\n";
  }
Osman Durdag
  • 955
  • 1
  • 7
  • 18