C++
Auto
David0903
2022. 2. 2. 23:45
1. auto
복잡한 형식의 변수 선언을 간소화
변수의 타입을 추론하여 결정하도록 지시
2. auto 사용 사례
auto PI = 3.14; // auto가 double로 자동 지정
auto n = 3;
auto *p = &n; // 변수 p는 int* 타입으로 자동 선언
int n = 10;
int & ref = n;
auto ref2 = ref;
3. iterator와 auto
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = { 1,2,3,4,5 };
for (auto it = v.begin(); it < v.end(); it++) {
int n = *it;
n = n * 2;
*it = n;
}
for (auto it = v.begin(); it < v.end(); it++) cout << *it << ' ';
cout << endl;
}