Reputation: 66184
I have an std::map
, and I would like to define an iterator that returns modified values. Typically, a std::map<int,double>::iterator
iterates over std::pair<int,double>
, and I would like the same behavior, just the double value is multiplied by a constant.
I tried it with boost::transform_iterator
, but it doesn't compile:
#include <map>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/functional.hpp>
typedef std::map<int,double> Map;
Map m;
m[100] = 2.24;
typedef boost::binder2nd< std::multiplies<double> > Function;
typedef boost::transform_iterator<Function,
Map::value_type*> MultiplyIter;
MultiplyIter begin =
boost::make_transform_iterator(m.begin(),
Function(std::multiplies<double>(), 4));
// now want to similarly create an end iterator
// and then iterate over the modified map
The error is:
error: conversion from 'boost
::transform_iterator<
boost::binder2nd<multiplies<double> >, gen_map<int, double>::iterator
, boost::use_default, boost::use_default
>' to non-scalar type 'boost::transform_iterator<
boost::binder2nd<multiplies<double> >, pair<const int, double> *
, boost::use_default, boost::use_default
>' requested
What is gen_map
and do I really need it?
I adapted the transform_iterator
tutorial code from here to write this code ...
Upvotes: 3
Views: 1285
Reputation: 4345
std::multiply which expect a double as first argument, and not a std::pair.
The transform function must take a single std::pair argument (as the map elements are pair of key values) and return whatever you want.
The following function could be used instead of std::multiply.
double times(std::pair<int,double> const& p, int i) {
return i*p.second;
}
boost::make_transform_iterator(m.begin(),
Function(times, 4));
Upvotes: 5
Reputation: 137810
typedef boost::transform_iterator<Function,
Map::iterator> MultiplyIter; // value_type* only occasionally
// works as an iterator
gen_map
appears to be the class name that underlies std::map
.
The tutorial uses int*
because the pointer is the proper type to iterate over a C-style array.
Upvotes: 2