473,387 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

boost::range problem.

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
2 2870

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Martin | last post by:
I am trying to write code that selects a random number in the range 0 to n, where n can be substantially greater than the RAND_MAX on my system which is 32767. I am using VC++ 2003 FWIW. As you...
16
by: Jeff Flinn | last post by:
At the risk of raising the OT ire of some here, I'd like to know what might be done to raise the awareness of the boost libraries. I've found boost and it's libraries invaluable in my work for ~5...
34
by: Guch Wu | last post by:
Boost has many terrific libraries. But I want to know whether they are ready for using in real projects. Which of them are mature enough, or just only in progress?
6
by: Olaf van der Spek | last post by:
Hi, I've got a lot of functions (compression, encryption, hashing, encoding, etc) that work on a memory range. I wrote my own class that has constructors for void*, void* void*, size_t...
6
by: Soren | last post by:
Hi! I'm trying to extend my python program with some C++ code. Right now I've spent hours just trying to get boost to work! I'm trying to get the example hello.cpp to work. Using Windows XP...
1
by: Sticker | last post by:
I use some boost lib function in VC++ within the VS.net IDE. For release model there seems no problem but I can not use the function in debug model. For example, erase_all() function leads to...
1
by: Noah Roberts | last post by:
A while back I posted a question either here or to the boost user list about how to iterate through a vector of strings and perform a lexical cast on them into elements of a tuple. I was working...
4
by: Daniel Pitts | last post by:
I have a template: template<typename c, unsigned size> struct Vector { c components; template<unsigned index> c &get() { return components; } }; Vector<double, 3vect;
0
by: Joey Mukherjee | last post by:
Hello! I am trying to use boost::multi_array and want to create a view where I am lowering the number of dimensions, but I wish the dimension to be reduced to be decided at runtime. I have a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.