Boost logo

Boost Users :

From: Antonio Piccolboni (antonio_piccolboni_at_[hidden])
Date: 2002-09-24 14:01:27


Dear Boosters

Suppose you have a matrix represented raw-wise with a vector< vector
<float > > and you want to iterate over the coloumns. Easy with the
projection adaptor. But life is never so easy. My matrix is
represented raw wise by a map <vector <float> > and I need the same
kind of iterator. There is no way I can get my code to compile. I
think the problem is that vector iterators return values, map
iterators retun pairs <key,value> (that is, references to the above).
And this seems not only to doom my attempt at defining this n-th dim
projection iterator, but even more so and fundamentally, it seem to
preclude my ultimate goal of defining a projection for any container1
<containr2 < element_type > > where container2 two is random access.
Any help would be appreciated and if I make it to the larger goal, I
will return it to the community. Thanks

Antonio

Header

#ifndef GRAPH_H
#define GRAPH_H

#include <boost/config.hpp>
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/iterator_adaptors.hpp>

//typedef pair<long, vector<float> > graph_element;
typedef vector<float> graph_element;

template <int index>
struct select_component {
  typedef pair < long, graph_element > argument_type;
  typedef float result_type;
  const float & operator()(const graph_element & r) const {
    return r.second[index];
  }
  float & operator()(graph_element & r) const {
    return r.second[index];
  }
};

#endif

Would be test code

#include "graph.h"
#include <map>
#include<vector>

int main(int, char*[])
{
  typedef map < long, graph_element > graph;

  graph aGraph;

  aGraph[0].push_back(0.00);
  aGraph[0].push_back(0.01);
  aGraph[0].push_back(0.02);
  aGraph[1].push_back(0.10);
  aGraph[1].push_back(0.11);
  aGraph[1].push_back(0.12);
  aGraph[2].push_back(0.20);
  aGraph[2].push_back(0.21);
  aGraph[2].push_back(0.22);
  aGraph[3].push_back(0.30);
  aGraph[3].push_back(0.31);
  aGraph[3].push_back(0.32);

  boost::projection_iterator_generator < select_component < 0 >,
    graph::iterator >::type
    graph_first(aGraph.begin()),
    graph_last(aGraph.end());
  
  copy(graph_first, graph_last,
            ostream_iterator<float>(cout, "\n"));
  cout << endl;
}

Post-modern error message

cd /home/apicco/src/analysis/transcriptome/transmap/
g++ -g -I/home/apicco/install/boost_1_28_0 -o graph-test graph-test.C
/home/apicco/install/boost_1_28_0/boost/iterator_adaptors.hpp: In
method `typename
IteratorAdaptor::reference
boost::projection_iterator_policies<AdaptableUnaryFunction>::dereferen
ce
(const IteratorAdaptor &) const [with IteratorAdaptor =
boost::iterator_adaptor<_Rb_tree_iterator<pair<const long int,
graph_element>, pair<const long int,
graph_element> &, pair<const long int, graph_element> *>,
boost::projection_iterator_policies<select_component<0> >, float,
float &, float *,
boost::detail::default_argument, boost::detail::default_argument>,
AdaptableUnaryFunction =
select_component<0>]':
/home/apicco/install/boost_1_28_0/boost/iterator_adaptors.hpp:867:
instantiated from `boost::iterator_adaptor<Base, Policies, Value,
Reference, Pointer, Category, Distance>::operator* () const [with
Base = _Rb_tree_iterator<pair<const long int, graph_element>,
pair<const long int, graph_element> &, pair<const long int,
graph_element> *>, Policies =
boost::projection_iterator_policies<select_component<0> >, Value =
float, Reference = float &, Pointer = float *, Category =
boost::detail::default_argument, Distance =
boost::detail::default_argument]'
/usr/include/g++-3/stl_algobase.h:129: instantiated from `__copy
(_InputIter, _InputIter, _OutputIter, input_iterator_tag, _Distance
*) [with _InputIter =
boost::iterator_adaptor<_Rb_tree_iterator<pair<const long int,
graph_element>, pair<const long int, graph_element> &, pair<const
long int, graph_element> *>,
boost::projection_iterator_policies<select_component<0> >, float,
float &, float *, boost::detail::default_argument,
boost::detail::default_argument>, _OutputIter =
ostream_iterator<float>, _Distance = ptrdiff_t]'
/usr/include/g++-3/stl_algobase.h:161: instantiated from
`__copy_dispatch<_InputIter, _OutputIter, _BoolType>::copy
(_InputIter, _InputIter, _OutputIter) [with _InputIter =
boost::iterator_adaptor<_Rb_tree_iterator<pair<const long int,
graph_element>, pair<const long int, graph_element> &, pair<const
long int, graph_element> *>,
boost::projection_iterator_policies<select_component<0> >, float,
float &, float *, boost::detail::default_argument,
boost::detail::default_argument>, _OutputIter =
ostream_iterator<float>, _BoolType = __true_type]'
/usr/include/g++-3/stl_algobase.h:188: instantiated from `copy
(_InputIter, _InputIter, _OutputIter) [with _InputIter =
boost::iterator_adaptor<_Rb_tree_iterator<pair<const long int,
graph_element>, pair<const long int, graph_element> &, pair<const
long int, graph_element> *>,
boost::projection_iterator_policies<select_component<0> >, float,
float &, float *, boost::detail::default_argument,
boost::detail::default_argument>, _OutputIter =
ostream_iterator<float>]'
graph-test.C:30: instantiated from here
/home/apicco/install/boost_1_28_0/boost/iterator_adaptors.hpp:1247:
no match for call to `(const
select_component<0>) (pair<const long int, graph_element> &)'
graph.h:19: candidates are: const float
&select_component<index>::operator() (const graph_element &) const
[with int index = 0]
graph.h:22: float &select_component<index>::operator
()
(graph_element &) const [with int index = 0]

Compilation exited abnormally with code 1 at Tue Sep 24 11:14:11


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net