1차원 동적 배열을 생성하기 위해서는
자바에서 배열을 생성할 때와 비슷하게 생성하면 됩니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<iostream> using namespace std; int main(){ int n; cin >> n; int *list = new int[n]; for(int i = 0; i < n; i++) cin >> list[i]; for(int i = 0; i < n; i++) cout << list[i]; return 0; } | cs |
하지만 2차원 동적 배열을 생성하기 위해서 []을 [][]로 바꾼다고 해결이 되지 않습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<iostream> using namespace std; int main(){ int n; cin >> n; int** arr = new int*[n]; for(int i = 0; i < n; i++) arr[i] = new int[n]; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cin >> arr[i][j]; } } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cout << arr[i][j]; } cout <<'\n'; } return 0; } | cs |
※ 본 글은 개인 포트폴리오 혹은 공부용으로 사용하기 때문에, 무단 복사 유포는 금지하지만, 개인 공부 용도로는 얼마든지 사용하셔도 좋습니다.
'C_C++ 프로그래밍 > C_C++ 프로그래밍' 카테고리의 다른 글
[C++] 선택정렬(Selected Sort) (0) | 2019.04.01 |
---|---|
[C++] pair sort (0) | 2019.03.21 |
[Algorithm] C++ Bitmask란? (0) | 2019.03.17 |
[C++] [Algorithm] C++에서 next_permutation 함수( prev_permutation 함수)를 통해서 순열 구하기 (0) | 2019.03.16 |
댓글