[#32][C++] 범위 기반 For문 사용
C++ 범위 기반 For문 사용
배열의 모든 요소 접근에 편리함
- 배열 요소 개수에 맞춰 출력하기
#include <iostream>
using namespace std;
int main() {
int a[5] = { 0,1,2,3,4 };
for (auto n : a) {
cout << n << endl;
}
return 0;
}
| cs |
- a의 내용을 바꾸고 싶을 때 for(auto &n : a)
#include <iostream>
using namespace std;
int main() {
int a[5] = { 0,1,2,3,4 };
for (auto &n : a) {
n = 5;
cout << n << endl;
}
return 0;
}
| cs |
댓글
댓글 쓰기