1. auto 키워드

C++11에서 auto 키워드는 컴파일러에게 타입을 알아내라고 지시하라는 의미. 즉, 프로그래머가 직접 타입을 지정하지 않아도 변수의 이름만 컴파일러에게 전달하면 변주에 지정된 값에 맞춰서 컴파일러가 자동으로 타입을 지정

auto i = 100; // i 변수의 타입은 int
auto l = 100L; // l 변수의 타입은 long
auto p = new Person(); // p 변수의 타입은 Person 포인터

특히, STL 컨테이너에서 반복자 등을 지정할 때는 auto 키워드가 입력해야할 코드 양을 상당히 줄여줌

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char** argv) 
{
    vector<int> vInt;
    for(auto i=0; i < 10; ++i) {
        vInt.push_back(i);
    }

    // C++03 표준
    cout<<"C++03 standard"<<endl;
    vector<int>::iterator it = vInt.begin();
    while(it != vInt.end()) {
        cout<<*it<<endl;
        it++;
    }

    // C++11 표준
    cout<<"C++11 standard"<<endl;
    auto it2 = vInt.begin();
    while(it2 != vInt.end()) {
        cout<<*it2<<endl;
        it2++;
    }
    getchar();
    return 0 ;
}