C++中用vector实现二维数组

 废话不多说,直接看实例代码就懂了

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int row = 3;
	int col = 3;
	int initValue = 6;
	vector<vector<int>> arr(row, vector<int>(col,initValue));
	arr[1][1] = 8;
	arr[0][0] = 8;
	arr[2][2] = 8;
	for (int i = 0; i < arr.size(); i++) {
		for (int j = 0; j < arr[0].size(); j++) {
			cout << arr[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}

运行结果如下

猜你喜欢

转载自blog.csdn.net/qq_31702609/article/details/107645350