본문 바로가기

CS/자료구조

<자료구조> 1차원 배열과 2차원 배열

2차원 배열은 단순 하나의 차원을 늘린것 뿐이다.

 

 

 

 

 

코드


#include<bits/stdc++.h>
using namespace std;
int a[5];
int temp[5];

const int x = 4;
const int y = 3;
int b[y][x];

		
int main()
{
	// 1차원 배열 
	for(int i = 0; i < 5; i++) a[i] = i;
	memcpy(temp, a, sizeof(a));
	for (int i : temp) cout << i << " "; // 0 1 2 3 4
	cout << endl;
	
	// 2차원 배열 
	for(int i = 0; i < y; i++)
	{
		for (int j = 0; j < x; j++)
			b[i][j] = (i + j);
	}
	
	for(int i = 0; i < y; i++)
	{
		for (int j = 0; j < x; j++)
			cout << b[i][j] << " ";
		cout << endl;
	}
	
	/*
	0 1 2 3
	1 2 3 4
	2 3 4 5
	*/
	
	return 0;
}

'CS > 자료구조' 카테고리의 다른 글

<자료구조> 우선순위 큐  (0) 2022.04.20
<자료구조> queue와 deque  (0) 2022.04.19
<자료구조> stack  (0) 2022.04.19
<자료구조> set과 multiset  (0) 2022.04.19
<자료구조> Unordered Map과 Map 차이  (0) 2022.02.15