- Gaining access to the STL list type
#include <list>
- Declaring a list of ints:
list<int> myList;
- Adding to the front or back of the list:
myList.push_back(10);
myList.push_front(1);
- Getting the value from the front or back of the list:
int valFront = myList.front();
int valBack = myList.back();
- Removing from the front or back of the list:
myList.pop_front();
myList.pop_back();
- And most importantly, iterating through the list:
for (list<int>::iterator i = myList.begin(); i != myList.end(); ++i)
{
cout << *i << endl;
}
-
For example, a recursive function to sum the elements of a list would be:
int listSum(list<int> l)
{
int front = l.front();
l.pop_front();
if (l.size != 0) return front + listSum(l);
return front;
}
These methods will let you perform most common operations on lists.