473,385 Members | 1,356 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,385 software developers and data experts.

range iterator

Hi,
I want ro iterate over a few container class within a range
specified, instead of begin & end.
How to construct a range class, which takes start & end, and iteration
is available within that range only. Itaration may be const,
bidiractional, forward or backward.

Say I have a vector or other container class,
like vector<intvec;
and want to return a range class like range(vec.begin()+5,
vec.end() - 10);
range will have a begin & end to denote start & end of the range,
but beyond the range is not allowed to be iteraded.
Actually, I have a large no of objects stored in a container, a range
of them belongs to a particular instance, who is allowed to modify it.
Those ranges are sometimes overlapping also.
some only needs a const iterator while other needs modification (may
not be deletion, which itself modifies the range!). I want each
instance to return it's associated range for iteration.
Thanks for any suggestion.

Aug 24 '06 #1
3 2827
toton wrote:
Hi,
I want ro iterate over a few container class within a range
specified, instead of begin & end.
How to construct a range class, which takes start & end, and iteration
is available within that range only. Itaration may be const,
bidiractional, forward or backward.

Say I have a vector or other container class,
like vector<intvec;
and want to return a range class like range(vec.begin()+5,
vec.end() - 10);
range will have a begin & end to denote start & end of the range,
but beyond the range is not allowed to be iteraded.
Actually, I have a large no of objects stored in a container, a range
of them belongs to a particular instance, who is allowed to modify it.
Those ranges are sometimes overlapping also.
some only needs a const iterator while other needs modification (may
not be deletion, which itself modifies the range!). I want each
instance to return it's associated range for iteration.
You could start with a simple adaptor like this:

template < typename Iterator >
class range {

Iterator start;
Iterator past_end;

public:

typedef typename iterator_traits< Iterator >::size_type size_type;

range ( Iterator const & from, Iterator const & to )
: start ( from )
, past_end ( to )
{}

size_type size ( void ) const {
typename iterator_traits< Iterator >::difference_type
dist = std::distance( this->start, this->past_end );
assert( dist >= 0 );
return ( dist );
}

Iterator begin ( void ) const {
return( start );
}

Iterator end ( void ) const {
return( this->past_end );
}

bool empty ( void ) const {
return( this->start == this->past_end );
}

}; // class range<>

The use of the assert is debatable. Ideally, one would check the validity of
the range upon construction and throw something if [from,to) is not sound.
However, that might be inefficient (for non-random access iterators) or
impossible (if from and to point to different containers).
Best

Kai-Uwe Bux
Aug 24 '06 #2

Kai-Uwe Bux wrote:
toton wrote:
Hi,
I want ro iterate over a few container class within a range
specified, instead of begin & end.
How to construct a range class, which takes start & end, and iteration
is available within that range only. Itaration may be const,
bidiractional, forward or backward.

Say I have a vector or other container class,
like vector<intvec;
and want to return a range class like range(vec.begin()+5,
vec.end() - 10);
range will have a begin & end to denote start & end of the range,
but beyond the range is not allowed to be iteraded.
Actually, I have a large no of objects stored in a container, a range
of them belongs to a particular instance, who is allowed to modify it.
Those ranges are sometimes overlapping also.
some only needs a const iterator while other needs modification (may
not be deletion, which itself modifies the range!). I want each
instance to return it's associated range for iteration.

You could start with a simple adaptor like this:

template < typename Iterator >
class range {

Iterator start;
Iterator past_end;

public:

typedef typename iterator_traits< Iterator >::size_type size_type;

range ( Iterator const & from, Iterator const & to )
: start ( from )
, past_end ( to )
{}

size_type size ( void ) const {
typename iterator_traits< Iterator >::difference_type
dist = std::distance( this->start, this->past_end );
assert( dist >= 0 );
return ( dist );
}

Iterator begin ( void ) const {
return( start );
}

Iterator end ( void ) const {
return( this->past_end );
}

bool empty ( void ) const {
return( this->start == this->past_end );
}

}; // class range<>

The use of the assert is debatable. Ideally, one would check the validity of
the range upon construction and throw something if [from,to) is not sound.
However, that might be inefficient (for non-random access iterators) or
impossible (if from and to point to different containers).
Yes, this is a good example. but someone can iterate past range, is it
not? like someone can go end+10 or start-5 also. Is there a way to
prevent out of range in iterator? I am not sure how to do it as the
iterator itself does not throw out_of_range.
Anything can be done to prevent out of range?
secondly, if I have a class instance which knows the range from the
constructor, is it better to store the range in object, and return the
range as reference? or store only the start & end location, and return
a constructed range object when range is needed?
Best

Kai-Uwe Bux
Aug 24 '06 #3

Kai-Uwe Bux wrote:
toton wrote:
Hi,
I want ro iterate over a few container class within a range
specified, instead of begin & end.
How to construct a range class, which takes start & end, and iteration
is available within that range only. Itaration may be const,
bidiractional, forward or backward.

Say I have a vector or other container class,
like vector<intvec;
and want to return a range class like range(vec.begin()+5,
vec.end() - 10);
range will have a begin & end to denote start & end of the range,
but beyond the range is not allowed to be iteraded.
Actually, I have a large no of objects stored in a container, a range
of them belongs to a particular instance, who is allowed to modify it.
Those ranges are sometimes overlapping also.
some only needs a const iterator while other needs modification (may
not be deletion, which itself modifies the range!). I want each
instance to return it's associated range for iteration.

You could start with a simple adaptor like this:

template < typename Iterator >
class range {

Iterator start;
Iterator past_end;

public:

typedef typename iterator_traits< Iterator >::size_type size_type;

range ( Iterator const & from, Iterator const & to )
: start ( from )
, past_end ( to )
{}

size_type size ( void ) const {
typename iterator_traits< Iterator >::difference_type
dist = std::distance( this->start, this->past_end );
assert( dist >= 0 );
return ( dist );
}

Iterator begin ( void ) const {
return( start );
}

Iterator end ( void ) const {
return( this->past_end );
}

bool empty ( void ) const {
return( this->start == this->past_end );
}

}; // class range<>

The use of the assert is debatable. Ideally, one would check the validity of
the range upon construction and throw something if [from,to) is not sound.
However, that might be inefficient (for non-random access iterators) or
impossible (if from and to point to different containers).
Yes, this is a good example. but someone can iterate past range, is it
not? like someone can go end+10 or start-5 also. Is there a way to
prevent out of range in iterator? I am not sure how to do it as the
iterator itself does not throw out_of_range.
Anything can be done to prevent out of range?
secondly, if I have a class instance which knows the range from the
constructor, is it better to store the range in object, and return the
range as reference? or store only the start & end location, and return
a constructed range object when range is needed?
thanks for the help.
Best

Kai-Uwe Bux
Aug 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chris | last post by:
Hey all. Anyone who is familiar with Python programming knows that you can have code like this: list = This code puts all the items processed by the for loop in a list plus 1. Is there a way...
17
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a...
29
by: Steve R. Hastings | last post by:
When you compile the expression for i in range(1000): pass does Python make an iterator for range(), and then generate the values on the fly? Or does Python actually allocate the list and...
0
by: toton | last post by:
Hi, I have a view class for a vector, which stores different range for a few vectors, and provides a way to iterate over the range. The range class takes a pair of iterator, as given in post...
2
by: toton | last post by:
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...
10
by: Alan Johnson | last post by:
24.1.1.3 says about InputIterators: Algorithms on input iterators should never attempt to pass through the same iterator twice. They should be single pass algorithms. In 24.3.4.4 summarizes the...
2
by: newbie | last post by:
Does standard say anything about this situation? or it is an undefined behavior? Thanks ------------------------------ map<int, inttest; test = 1; test = 2; test = 3;
4
by: Aaryan123 | last post by:
Function to check a number between a range in perl ========================================= Back ground: This situation has been fixed in Perl5.005. Use of .. in a for loop will iterate over...
5
by: subramanian100in | last post by:
Whenever we specify an iterator range to a container operation or an std::algorithm, the first iterator should always be a valid iterator ie it should point to some element in the respective...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.