// example of iterating over a list // // build with: g++ -std=c++11 list_iterator.cpp // expected output: // one two three four // one two 3 four #include #include #include using namespace std; int main() { list words; // note this code would work with vector, etc. words.push_back("one"); words.push_back("two"); words.push_back("three"); words.push_back("four"); // I can count! for(auto w : words) cout << w << " "; cout << endl; // find "three" and change it to "3": auto wordlist_iterator = words.begin(); while ( wordlist_iterator != words.end() && *wordlist_iterator != "three" ) ++wordlist_iterator; if ( wordlist_iterator != words.end() ) *wordlist_iterator = "3"; // print list, this time using explicit iteration: for(auto wit = words.begin(); wit != words.end(); ++wit) cout << *wit << " "; cout << endl; return 0; }