阅读量:0
- 利用嵌套vector进行初始化:
#include <vector> std::vector<std::vector<int>> vec2d = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
- 使用resize()函数初始化指定大小的二维向量:
#include <vector> int rows = 3; int cols = 3; std::vector<std::vector<int>> vec2d; vec2d.resize(rows, std::vector<int>(cols, 0));
- 使用循环初始化二维向量:
#include <vector> int rows = 3; int cols = 3; std::vector<std::vector<int>> vec2d(rows, std::vector<int>(cols)); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { vec2d[i][j] = i * cols + j + 1; } }