2012-06-23 2 views

답변

8

이 컴파일하면 사용하고있는 C++ (11)는, 다음과 같은 수 있습니다 :

for (string& feature : features) { 
    // do something with `feature` 
} 

This is the range-based for loop.

이 기능을 돌연변이하지 않으려면 string const& (또는 단지 string으로 선언 할 수 있지만 불필요한 복사가 발생합니다).

22

이 시도 :

for(vector<string>::const_iterator i = features.begin(); i != features.end(); ++i) { 
    // process i 
    cout << *i << " "; // this will print all the contents of *features* 
} 

당신이 C++ (11)를 사용하는 경우,이 법률 너무 :

for(auto i : features) { 
    // process i 
    cout << i << " "; // this will print all the contents of *features* 
} 
+0

아마도 당신은'++ i'를 의미하고'i ++'를 의미하지는 않습니다. –

+0

사실 그것은 같은 것입니다. –

+7

[아니요, 그렇지 않습니다!] (http://stackoverflow.com/questions/24901/is-there-a-performance-difference-between-i--i-in-c) 'const_iterator'는'iterator'만이 아닙니다. 이것은 보일러 플레이트 코드입니다. 자고있을 때에도 질문을 받더라도 올바르게 이해할 수있을만큼 잘 배워야합니다. –

관련 문제