|
Boost Users : |
From: gast128 (gast128_at_[hidden])
Date: 2006-07-24 06:04:52
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<std::string, double> StringDoubleMap;
typedef boost::function<StringDoubleMap::key_type (const
StringDoubleMap::value_type&)> Function;
typedef boost::transform_iterator<Function, StringDoubleMap::const_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
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net