Nifty iterators
Couple of different ways of iterating over collections:
The long way
for (collection<T>::iterator it = t.begin(); it != t.end(); ++it) { // Do stuff with *it }
With template functions
void function(const T& t) { // Do stuff with t } std::for_each(t.begin(), t.end(), function);
c++11 lambdas
std::for_each(t.begin(), t.end(), [](const T&) { // Do stuff with t });
Or just over a collection
for (const T& value, t) { // Do stuff with value }
Of these, the first one frustrates me sometimes since you need to know what type of collection you’re iterating over. Thankfully c++11 introduced type inference with the auto keyword. Means you can instead write
for (auto it = t.begin(); it != t.end(); ++it) { // Do stuff with *it }