Friday, May 16, 2008

Improve your code by using boost::transform_iterator with STL map and multimap

Often while using STL, you want to select the key or value while iterating through the map, but the STL interface yield a pair upon derefencing the iterator. This makes the interface of your class while your implementation uses map, different from interface using vector. This is undesirable.

No worry, you can return a boost::transform_iterator. boost::transform_iterator is a useful tool to transform the value of the iterator while dereferencing it. The most common use of boost::transform_iterator is in the situation described above, although it is more general. For using boost::transform_iterator as above, you will need functions select_first and select_second as implemented here.

Always cheers while coding in C++!