본문 바로가기
카테고리 없음

[c++11] for문 auto 사용하기, 선호하는 방법

by juheeL 2017. 6. 20.

auto num = 1;

auto str = "Hello";


cout << "type : " < typeid(num).name() << endl;

cout << "type : " < typeid(str).name() << endl;


for(vector<int>::iterator iter = vec.begin(); iter != vec.end(); ++iter)

{

cout<< *iter <<endl;

}


for(auto iter = vec.begin(); iter != vec.end(); ++iter)

{

cout << *iter << endl;

}




* 선호되는 방법 2가지

// vec 요소 변경 가능할 때 - 선호

for(auto &each : vec)

{

each++;

}


// vec 요소 변경 불가할 때 - 선호

for(const auto &each : vec)

{

cout<< each << endl;

}




출처 : 


https://msdn.microsoft.com/ko-kr/library/jj203382.aspx


http://blog.naver.com/PostView.nhn?blogId=hextrial&logNo=220206968604&categoryNo=38&parentCategoryNo=0&viewDate=&currentPage=1&postListTopCurrentPage=1&from=section