- Gaining access to the STL vector type
#include <vector>
- Declaring a vector of ints:
vector<int> myVec;
- Adding to the back of the vector:
myVec.push_back(10);
- Getting or setting a value in a vector (just like an array)
int val = myVec[5];
myVec[2] = 42;
- Iterating through the vector:
unsigned int size = myVec.size();
for (unsigned int i = 0; i < size; i++)
{
cout << myVec[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;
}