GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Wednesday, March 16, 2011

C++ - List Of STL

Declaring a list:
list<string> city;

For inserting data to the list at the last position, use:
city.push_back("London");
city.push_back("Singapore");
city.push_back("Sydney");

For inserting data to the list at the first position, use:
city.push_front("Tokyo");

For removing an element at the last position, use:
city.pop_back();

For removing an element at the first position, use:
city.pop_front()

To get the first inserted element, use:
cout << city.front();

To get the last inserted element, use:
cout << city.back()

To iterate all of the elements, use:
list<string>::iterator it;
for(it = city.begin(); it != city.end(); it++){
     cout << *it << endl;
}
Share/Bookmark

No comments:

Post a Comment