By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,070 Members | 1,718 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,070 IT Pros & Developers. It's quick & easy.

boost::range problem.

P: n/a
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<intVI;
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::iteratorr(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_iteratorr(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

Nov 16 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

toton wrote:
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 .
#include <iostream>
#include <vector>
#include <boost/range.hpp>
#include <boost/range/iterator_range.hpp>

typedef std::vector<intVI;

// range_finder
boost::iterator_range< VI::const_iterator >
range_finder( VI::const_iterator begin,
VI::const_iterator end )
{
return boost::make_iterator_range(begin, end);
}
typedef vector<intVI;
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::iteratorr(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_iteratorr(v.begin( )+1,v.begin()+3);
ok, but you can't reuse r:

boost::iterator_range< VI::const_iterator >
q(range_finder(v.begin(),v.end()));
std::copy(q.begin(),q.end(), std::ostream_iterator<int>(std::cout,"
"));
std::cout << std::endl;
cout<<r[1]<<endl; ///it should print 1.
std::cout << *q.begin() << std::endl;
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
Nov 16 '06 #2

P: n/a

Salt_Peter wrote:
toton wrote:
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 .

#include <iostream>
#include <vector>
#include <boost/range.hpp>
#include <boost/range/iterator_range.hpp>

typedef std::vector<intVI;

// range_finder
boost::iterator_range< VI::const_iterator >
range_finder( VI::const_iterator begin,
VI::const_iterator end )
{
return boost::make_iterator_range(begin, end);
}
typedef vector<intVI;
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::iteratorr(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_iteratorr(v.begin( )+1,v.begin()+3);

ok, but you can't reuse r:
Yes, those are copy paste from separate test program.
boost::iterator_range< VI::const_iterator >
q(range_finder(v.begin(),v.end()));
std::copy(q.begin(),q.end(), std::ostream_iterator<int>(std::cout,"
"));
std::cout << std::endl;
cout<<r[1]<<endl; ///it should print 1.

std::cout << *q.begin() << std::endl;
It doesn't specify the problem I am facing for the const version. Your
program is possible, and I had already tested something similar.
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
The main problem is, I can apply indexing operator over an random
access iterator, be it const or non const. In const_iterator it returns
a const_reference while for non const iterator it returns reference.
Something like this,
typedef vector<intVI;
VI v;
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, " "));
cout<<endl;
const VI& cv = v; ///a const reference
VI::iterator it = v.begin();//non const iterator
it[1] = 100; //returns reference, l value assignment is possible.
copy(v.begin(),v.end(),ostream_iterator<int>(cout, " "));
VI::const_iterator it1 = v.begin(); // const version, it can even be
cv.begin()
cout<<it1[1]<<endl; ///returns a const_reference , not value_type.
Assignment is not possible.

However for both range_iterator and sub_range the indexing operator is
not working for me. It looks they are not valuing const.
Here for sub_range version,
boost::sub_range<VIr(v.begin()+1,v.begin()+3); ///I got a sub_range
directly from container.
r[1] = 201; cout<<r[1]<<endl; /// range assignment is possible, it
returns reference
boost::sub_range<const VIr1(cv.begin()+1,cv.end()+3); /// const
version.Is it like range constructed from const_iterator ? It can well
be r1(v.begin()+1,v.end()+3); ?
copy(r1.begin(),r1.end(),ostream_iterator<int>(cou t," "));
cout<<r1[1]<<endl; ///It should return a const_reference , or
value_type ? I dont care.
But this line is not compiling.
It gives error like
cannot convert from 'const std::allocator<_Ty>::value_type' to
'boost::iterator_range<IteratorT>::value_type &'
Why indexed access should not be allowed here ? Only l value assignment
should not be allowed.
copy(r1.begin(),r1.end(),ostream_iterator<int>(cou t," "));
Am I making some mistake in the statement boost::sub_range<const VI>
assuming it is a const_sub_range kind of thing ?. i.e range of
const_iterator ? If, then what is the const_iterator counter part of
sub_range and range_iterator ?
abir
Nov 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.