



(22 votes)/* basic_stl_list.cpp by detour@metalshell.com * * Example of how to use the list provided in the STL. * * http://www.metalshell.com * */ #include <iostream> #include <string> #include <list> #include <algorithm> using namespace std; void print(string &value) { cout << value << endl; } int main() { list<string> myList; /* Push the string to the front of the list. Becuase this is the first item on the list, it performs the same as push_back would */ myList.push_front("item number #1"); /* Push the string to the back of the list, directly after "item number #1" */ myList.push_back("item number #3"); /* Find is part of the <algorithm> functions. It takes the beginning and end of your list and finds the first occurence of your search item */ list<string>::iterator IT = find(myList.begin(),myList.end(),"item number #3"); /* Insert "item number #2" directly in front of "item number #3". The position of "item number #3" is the iterator IT */ myList.insert(IT,"item number #2"); /* For_each is also part of the <algorithm> library. It takes the beginning and end of your list and calls the function you define. Your function must take the correct value type as an argument */ for_each(myList.begin(),myList.end(),print); // Remove "item number #1" myList.pop_front(); // Remove "item number #3" myList.pop_back(); cout << "-------------" << endl; for_each(myList.begin(),myList.end(),print); return 0; }