[c++] 2차원 vector 사용법

Roh·2023년 2월 6일
0
post-thumbnail

Ex) 1.


#include <iostream>
#include <vector>

using namespace std;

int main() {

	vector<vector<int>> v;

	v.push_back({ 1,1,1,1,1 });
	v.push_back({ 2,2,2,2,2 });
	v.push_back({ 3,3,3,3,3 });
	v.push_back({ 4,4,4,4,4 });
	v.push_back({ 5,5,5,5,5 });

	for (const auto v1 : v) {
		for (const auto v2 : v1)
			cout << v2 << " ";
		cout << endl;
	}

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

	
		
	return 0;
}

/*  Console

1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

*/

Ex) 2.


#include <iostream>
#include <vector>

using namespace std;

int main() {

	vector<vector<int>> vec;
	
	vector<int> v1;
	vector<int> v2;
	vector<int> v3;
	vector<int> v4;
	vector<int> v5;

	v1.push_back(1);
	v1.push_back(1);
	v2.push_back(2);
	v2.push_back(2);
	v3.push_back(3);
	v3.push_back(3);
	v4.push_back(4);
	v4.push_back(4);
	v5.push_back(5);
	v5.push_back(5);

	vec.push_back(v1);
	vec.push_back(v2);
	vec.push_back(v3);
	vec.push_back(v4);
	vec.push_back(v5);
	
	for (const auto& vec1 : vec) {
		for (const auto& vec2 : vec1)
			cout << vec2 << " ";
		cout << endl;
	}

	return 0;
}

/* Console

1 1
2 2
3 3
4 4
5 5

*/
profile
Better than doing nothing

0개의 댓글