Dear all,
in our company we use the STL map frequently. However one often wants to
iterate over the key_type only (or mapped type, or make desicions for the key
type, and take action on its corresponded map type).
With the transform_iterator this is possible, but the resulting code is not an
improvement over writing the loop explicitly. For example I want to search for
the maximum string length:
typedef std::map StringDoubleMap;
typedef boost::function Function;
typedef boost::transform_iterator
TransformIterator;
StringDoubleMap mp = boost::assign::map_list_of("George", 0.0)
("Bill", 0.0)
("Ronald", 0.0)
("Jimmy", 0.0);
Function fc = boost::bind(&StringDoubleMap::value_type::first, _1);
TransformIterator itBegin(mp.begin(), fc);
TransformIterator itEnd(mp.end(), fc);
TransformIterator it = std::max_element(itBegin, itEnd, boost::bind
(&std::string::length, _1) < boost::bind(&std::string::length, _2));
if (it != itEnd)
{
std::cout << it->c_str() << ' ' << it->length() << std::endl;
}
A select1st or select2nd would already a great help.
Wkr,
me