T
toton
Hi,
I am trying to use boost::range with one of my own container class,
and having some problem.
I am missing some usage of range. Can anyone suggest a proper way for
it ?
To show the problem I am facing, giving a small test program to
demonstrate .
typedef vector<int> VI;
VI v; //vector of int.
///storing 4 int to it.
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
copy(v.begin(),v.end(),ostream_iterator<int>(cout," ")); ///it prints
1 2 3 4
boost::iterator_range<VI::iterator> r(v.begin()+1,v.begin()+3); // a
range 2,3
copy(r.begin(),r.end(),ostream_iterator<int>(cout," ")); /// prints 2
3
r[1] = 100; /// change a range value from 3 => 100
copy(r.begin(),r.end(),ostream_iterator<int>(cout," ")); /// prints 2
100
Now doing the same for const_iterator
boost::iterator_range<VI::const_iterator> r(v.begin()+1,v.begin()+3);
cout<<r[1]<<endl; ///it should print 1.
here it says,
cannot convert from 'const std::allocator<_Ty>::value_type' to
boost::iterator_range<IteratorT>::value_type &
Here r[1] supposed to return a const_reference, I think, and
const_iterator indexing should return const_reference to the value, and
same for a const vector.
However, boost wants to return a reference here (not a const_reference
) and it gets a value type.
Any alternative way to handle the indexing, other than r.begin()+1 ?
The boost method signature is,
value_type& operator[]( size_type sz )const;
Thanks
abir
I am trying to use boost::range with one of my own container class,
and having some problem.
I am missing some usage of range. Can anyone suggest a proper way for
it ?
To show the problem I am facing, giving a small test program to
demonstrate .
typedef vector<int> VI;
VI v; //vector of int.
///storing 4 int to it.
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
copy(v.begin(),v.end(),ostream_iterator<int>(cout," ")); ///it prints
1 2 3 4
boost::iterator_range<VI::iterator> r(v.begin()+1,v.begin()+3); // a
range 2,3
copy(r.begin(),r.end(),ostream_iterator<int>(cout," ")); /// prints 2
3
r[1] = 100; /// change a range value from 3 => 100
copy(r.begin(),r.end(),ostream_iterator<int>(cout," ")); /// prints 2
100
Now doing the same for const_iterator
boost::iterator_range<VI::const_iterator> r(v.begin()+1,v.begin()+3);
cout<<r[1]<<endl; ///it should print 1.
here it says,
cannot convert from 'const std::allocator<_Ty>::value_type' to
boost::iterator_range<IteratorT>::value_type &
Here r[1] supposed to return a const_reference, I think, and
const_iterator indexing should return const_reference to the value, and
same for a const vector.
However, boost wants to return a reference here (not a const_reference
) and it gets a value type.
Any alternative way to handle the indexing, other than r.begin()+1 ?
The boost method signature is,
value_type& operator[]( size_type sz )const;
Thanks
abir