I’m having problems using boost::transform_iterators where “ordinary” iterators are expected. For instance, I want to add all keys from a map to a set. I wrote the following short snippet:
template <typename K, typename V>
struct map_keys {
typedef const K& result_type;
const K& operator()(const std::pair<K,V>& kvp) const {
return kvp.first;
}
};
int main() {
std::map<int, double> my_map;
std::set<int> my_set;
my_map[1]=1.2;
my_map[2]=2.4;
my_map[4]=4.1;
my_map[6]=12.2;
my_map[123]=3;
typedef map_keys<int, double> mk;
auto b = boost::make_transform_iterator(my_map.begin(), mk()),
e = boost::make_transform_iterator(my_map.end(), mk());
my_set.insert(b,e);
return 0;
}
After the insert, my_set contains a single value, -858993460 or 0xcccccccc. Why? If I print *b in a loop, all the values are printed as expected.
Ok, solved it myself suddenly. The problem is the typedef
result_type(which is not mentioned in the documentation, but unless I have it, the code does not compile (result_typeis not a member ofmap_keys<K,V>)). If I instead typedef it asIt works. I suppose as it was written earlier, it returned the address of something?