I\'m looking for practical and educational samples of C++ / STL code fitting in few lines. My actual favorites are:
Empty a vector freeing its reserved memory:<
Using std::for_each in combination with a lambda function (since C++11)
std::vector v(20); std::for_each( v.begin(), v.end(), [] (int item) { std::cout << item; } );
instead of
for(std::vector::const_iterator it = v.begin(); it != v.end(); ++it) { std::cout << *it; }
makes for better looking loops.