Nice insight on how to correctly write a boost::transform_iterator
for std::map<K,V>
. The thing to keep in mind is that std::map<K,V>::value_type
is in fact std::pair<K const, V>
instead of std::pair<K, V>
. The interesting snippet from the best answer reads:
The problem is that the functor object takes a reference to
pair<K,V>
, but is being passed a reference to themap
's value type,pair<const K,V>
. This requires a conversion, which requires copying both the key and the value to a new pair.
This fact could be crucial when using non-copyable objects, e.g., std::unique_ptr
as keys. The correct function object is as follows:
template<typename K, typename V>
struct get_value {
V const& operator()(std::pair<K const, V> const& pair) const {
return pair.second;
}
};
[Source]