vector創建二維數組並輸入

//利用STL中vector
vector<vector<int> > vis(m, vector<int>(n, 0));
#include<iostream>
#include<vector>
using namespace std;
 
void main()
{
	int r = 0, c = 0;
	cout << "Enter r: ";//規定二維數組行數
	cin >> r;
	cout << "Enter c: ";//規定二維數組列數
	cin >> c;
 
	vector<vector<int>>array;//定義二維數組
	vector<int>v;//定義一維數組
	array.clear();//將二維數組清空,或初始化(初始化代碼很簡單,不會可以百度)//也可不用這一步
	int temp=0;
	for (int i = 0; i < r; i++)//輸入r*c的二維數組
	{
		v.clear();//子數組返回時要清除
		for (int j = 0; j < c; j++)
		{
			cin >> temp;
			v.push_back(temp);
 
		}
		array.push_back(v);
	}
 
	for (int i = 0; i < r; i++)//打印輸入的二維數組
	{
		for (int j = 0; j < c; j++)
		{
			cout << array[i][j] << " ";
 
		}
		printf("\n");
	}
	while (1);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章