배열
배열(array)
- 같은 종류의 데이터들이 순차적으로 메모리에 저장되는 자료 구조
- 각각의 데이터(요소)들은 인덱스(번호)를 사용하여 독립적으로 접근 가능
- 대용량의 데이터를 동일한 이름으로 쉽게 저장하고 처리 가능
)
#include <iostream> using namespace std; int main() { const int STUDENTS = 10; int scores[STUDENTS]; int sum = 0; int i, average; for (i = 0; i < STUDENTS; i++) { cout << "학생들의 성적을 입력하시요: "; cin >> scores[i]; } for (i = 0; i < STUDENTS; i++) { sum += scores[i]; } average = sum / STUDENTS; cout << "성적 평균= " << average << endl; return 0; }
학생들의 성적을 입력하시요:
10
학생들의 성적을 입력하시요:20
학생들의 성적을 입력하시요:30
학생들의 성적을 입력하시요:40
학생들의 성적을 입력하시요:50
학생들의 성적을 입력하시요:60
학생들의 성적을 입력하시요:70
학생들의 성적을 입력하시요:80
학생들의 성적을 입력하시요:90
학생들의 성적을 입력하시요:100
성적 평균= 55
배열의 초기화
)
)
)
)
#include <iostream>
using namespace std;
int main()
{
const int STUDENTS = 5;
int scores[STUDENTS] = {
100, 200, 300, 403, 555
};
int sum = 0;
int i;
double average;
for (i = 0; i < STUDENTS; i++) {
sum += scores[i];
}
//average = sum / STUDENTS; //정수형 반환
average = sum / (double)STUDENTS;
cout << "성적 평균= " << average << endl;
return 0;
}
성적 평균= 311.6
elements 개수가 초기화 배열 개수보다 많은 경우는 syntax error
적으면 0으로 초기화
범위 기반 for 문
#include <iostream> using namespace std; int main() { int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int sum = 0; for (int i : list) { sum += i; } cout << sum << endl << endl; for (int& i : list) { cout << i << " "; } cout << endl; for (auto& i : list) { cout << i << " "; } return 0; }
55
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
int sum=0; int total = sum;
다른 공간 : 값의 복사
int sum=0; int& total = sum;
같은 공간(참조변수) : 값의 복사 X (overhead를 줄여준다.)
다 차원 배열
2차원 배열
)
)
다 차원 배열의 초기화
#include <iostream> using namespace std; #define WIDTH 9 #define HEIGHT 3 int main() { int table[HEIGHT][WIDTH]; int r, c; for (r = 0; r < HEIGHT; r++) { for (c = 0; c < WIDTH; c++) { table[r][c] = (r + 1) * (c + 1); } } for (r = 0; r < HEIGHT; r++) { for (c = 0; c < WIDTH; c++) { cout << table[r][c] << ", "; } cout << endl; } return 0; }
1, 2, 3, 4, 5, 6, 7, 8, 9,
2, 4, 6, 8, 10, 12, 14, 16, 18,
3, 6, 9, 12, 15, 18, 21, 24, 27,
예제 (배열 복사)
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { int list[]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int list2[10]; // list2 = list; // 문법 에러, = 연산자로 배열의 값이 복사되지 않음 // 배열의 크기 계산 방법 int length = sizeof(list) / sizeof(int); // 40 / 4 => 10 // list의 메모리크기: int 크기(4) * 10개 --> 40byte cout << length; // 복사전 list2 출력 for (auto i : list2) { cout << i << " "; } cout << endl; // list의 값을 list2로 복사 해보세요. for(int i=0; i<length; i++) { list2[i] = list[i]; } // 복사된 list2를 출력하세요. for (auto i : list2) { cout << i << " "; } cout << endl; cout << list << endl; return 0; }
1015145552 0 16 0 0 0 4200263 0 4199744 0
1 2 3 4 5 6 7 8 9 10
0x61fda0
템플릿 만드는 법
Manage
> User Snippets
> c++ 검색
> cpp.json
{
// Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
"c++ main function": {
"prefix": "c++_main",
"body": [
"#include <iostream>",
"using namespace std;",
"",
"int main(int argc, char const *argv[]) {",
" $0",
" return 0;",
"}"
],
"description": "c++ main function"
}
}
$0
는 커서 위치
'IoT 디바이스 활용 > C++' 카테고리의 다른 글
C++ - 문자열 (0) | 2020.10.11 |
---|---|
C++ - 함수 (0) | 2020.10.11 |
C++ - 제어 구조 (0) | 2020.10.09 |
C++ - 기초 사항 (0) | 2020.10.09 |
C++ - 개발환경 구축 (0) | 2020.10.09 |
댓글