Boost logo

Boost :

From: Alexander Belopolsky (belopols_at_[hidden])
Date: 2001-07-10 12:55:41


I am trying to use iterator_adaptor library to
cleate a key and value iterator for std::map.
It works fine for value iterator, but for key
iterator I get compilation errors.

Here is my approach:

// Create AdaptableUnaryFunctions
template <typename T>
struct select_first {
  typedef T argument_type;
  typedef typename T::first_type result_type;
  
  const result_type& operator()(const argument_type& x) const {
    return x.first;
  }

  result_type& operator()(argument_type& x) const {
    return x.first;
  }
};

template <typename T>
struct select_second {
  typedef T argument_type;
  typedef typename T::second_type result_type;
  
  const result_type& operator()(const argument_type& x) const {
    return x.second;
  }

  result_type& operator()(argument_type& x) const {
    return x.second;
  }
};

int main()
{
  typedef map<int, int> MAP;
  MAP m;

  m[1] = 10;
  m[2] = 20;
  m[3] = 30;

  // This works.
  boost::projection_iterator_generator<select_second<MAP::value_type>,
MAP::iterator>::type
    b(m.begin()), e(m.end());
  
  copy(b, e, ostream_iterator<int>(cout, "\n"));

  // This does not ...
  boost::projection_iterator_generator<select_first<MAP::value_type>,
MAP::iterator>::type
    b1(m.begin()), e1(m.end());
  
  copy(b1, e1, ostream_iterator<int>(cout, "\n"));
  return 0;
}

The problem seems to be in determining the correct reference type
which should be const int& in the case of key iterator,
but iterator_adaptor library makes it int&.

Please help.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk