C++嵌套vector(二维矩阵)的初始化

  • Post author:
  • Post category:其他


初始化一个嵌套的char型嵌套vector,已知C++11允许vector用花括号{}的初始方式,vector元素以逗号隔开,例如:

vector<char>> board = {'X', 'X', 'X'};

推广之,易知嵌套vector就是内层vector作为外层vector的元素,初始化用两层花括号,第一层花括号之间用逗号隔开:

vector<vector<char>> board = {{'X','.','.','X'},{'.','.','.','X'},{'.','.','.','X'}};

输出board:

int m = board.size();
    int n = board[0].size();
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cout<< board[i][j]<<" ";
        }
        cout<<endl;
    }

结果:

X . . X 
. . . X 
. . . X 



版权声明:本文为u012033124原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。