views:

201

answers:

2

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 ...

+2  A: 
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.

Potatoswatter
+4  A: 

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));
Vicente Botet Escriba
Does the `typedef` for Function need to change for this?
Frank
Yes it is needed to change it from value_type to iterator type as answered by Potatowatter.
Vicente Botet Escriba