How to use boost::transform_iterator to iterate over modifed std::map values?

Posted by Frank on Stack Overflow See other posts from Stack Overflow or by Frank
Published on 2010-05-01T01:39:49Z Indexed on 2010/05/01 1:47 UTC
Read the original article Hit count: 241

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

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost