Excellent!
If you are familiar with/have used <functional>, then you might also be interested in reading up
on the boost::lambda library. In some cases*, you can avoid the extra named function altogether
by using a lambda expression. For example:
1 2 3 4 5 6 7 8
|
typedef std::map<char, char> CharMap;
CharMap charMap;
//...
std::for_each( charMap.begin(), charMap.end(),
std::cout << (&boost::lambda::_1->*&CharMap::value_type::first) << " -> "
<< (&boost::lambda::_1->*&CharMap::value_type::second) << '\n' );
| |
Outputs the entire contents of the map, without having to write operator<< for std::pair<char, char>.
*lambda expressions can get quite complicated, and despite lambda support for doing almost anything (if-else, switch, while, try, catch, etc), often times for readability's sake it is better to create a named function anyway.