boost::range problem.

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
 
S

Salt_Peter

toton said:
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<int> VI;

// 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<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);

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::eek:stream_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
 
T

toton

Salt_Peter said:
toton said:
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<int> VI;

// 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<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);

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::eek:stream_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<int> VI;
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<VI> r(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 VI> r1(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>(cout," "));
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>(cout," "));
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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top