473,785 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2852
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_t ype
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_t ype
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_t ype
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
282
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 to do this in C++? Something like: int list = { x + 1 for (int x = 0; x < 5; x++); } Or something?
17
3059
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 counter within the loop declaration, for loops have always seemed to me to be about doing something a certain number of times, and not about iterating over an object. The reason for this distinction comes from the fact that I read a lot how...
29
2880
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 then step through it? I was under the impression that recent releases of Python optimize this
0
1422
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 http://groups.google.com/group/comp.lang.c++/browse_thread/thread/6d41708a9929e71a/a282fc1288b1231f?lnk=gst&q=range&rnum=2#a282fc1288b1231f However, as the range class do not have a default ctor, and range is not known when view class is constructed...
2
2909
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 I am facing, giving a small test program to demonstrate . typedef vector<intVI; VI v; //vector of int.
10
4739
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 effects of std::distance as: Effects: Returns the number of increments or decrements needed to get from first to last. The wording of the latter seems somewhat ambiguous. Does it mean that it actually performs the necessary increments or...
2
1589
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
8371
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 the range, without creating the entire range. for my $i (5 .. 500_005) { push(@results, some_func($i)); }
5
2389
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 container? Am I correct? For example, suppose vector<stringv; v.erase(v.begin(), v.end());
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10096
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.