Realizing two-dimensional array with vector in C++

 Not much nonsense, just look at the example code to understand

#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;
}

The results are as follows

 

Guess you like

Origin blog.csdn.net/qq_31702609/article/details/107645350