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

How ro Iterate a portion of a container

Hi,
I have a container class, and I want to iterate over a portion of the
container class while I insert/remove item from it. Noting down the
present location & constructing iterator from there is working, however
storing a copy of the iterator itself do not work.
For example,
If I have a vector of int.
vector<intv;
v.push_back(5);
v.push_back(10);
I pushed back two element into it.
Iterate over the vector from begin to end.
typedef vector<int>::iterator VecIterator;
for(VecIterator it = v.begin() ; it!= v.end(); ++it){
cout<<*it <<" ";
}
mark the current end as beginning of my range.
VecIterator beg1 = v.end();
Insert few more elements
v.push_back(12);
v.push_back(15);
now marke the end.
VecIterator end1 = v.end();
Iterate over the range.
for(VecIterator it = beg1 ; it!= end1; ++it){
cout<<*it <<" ";
}
This code do not work. I need to return a range (a pair of iterator for
a big container) for processing only, and a class stores the pair of
iterators for operation.
However changing the code to
int size = v.size();
v.push_back(12);
v.push_back(15);
VecIterator beg1 = v.begin()+size;
VecIterator end1 = v.end();
works.
However if the container class is not a vector, size may not give the
current location in the container. As in my case I have a custom
circular_buffer container, which can write over a fully occupied vector
also. In that case size may be different from the present location.
In general case how to mark such range. In my program, one large vector
contains the data, and different class are supposed to hold different
view of the data (i.e different range for operation). Also as the range
contains two iterator, and the iterator do not have a default ctor, it
is not possible to store them as class member (I store them as pointer
), as the range is not known at the construct time, but assigned later.
Is it a good way to store the range? or will I need to store raw
position in terms of size_t and return an newly constructed range every
time it is called from the position?
In that case the code may be little complex, as in my container the
positions are relative, and wrapped one rather than linear in case of
vector.

Any help is appreciated.

Sep 11 '06 #1
3 1905
"toton" <ab*******@gmail.comwrites:
v.push_back(12);
v.push_back(15);
now marke the end.
VecIterator end1 = v.end();
Iterate over the range.
for(VecIterator it = beg1 ; it!= end1; ++it){
cout<<*it <<" ";
}
This code do not work.
Pushing back beyond the current capacity in the vector invalidates
iterators. Try reserving enough capacity first (with the reserve
member function). Another way is to use std::list, as its iterators
stay valid under insertion and removal (except removal of the element
your iterator points to).

Cheers,

Jens
Sep 11 '06 #2

Jens Theisen wrote:
"toton" <ab*******@gmail.comwrites:
v.push_back(12);
v.push_back(15);
now marke the end.
VecIterator end1 = v.end();
Iterate over the range.
for(VecIterator it = beg1 ; it!= end1; ++it){
cout<<*it <<" ";
}
This code do not work.

Pushing back beyond the current capacity in the vector invalidates
iterators. Try reserving enough capacity first (with the reserve
member function). Another way is to use std::list, as its iterators
stay valid under insertion and removal (except removal of the element
your iterator points to).

Cheers,

Jens
Thanks for the reply. with reserve it works fine. However the main
reason is that iterator do not keep the present index in the vector.
rather it keeps a pointer to the present location. Thus when capacity
increases it fails to work. Is it a standard behavior?

I do not use vector in my implementation I use a circular buffer, which
is something like deque but with a reserve facility. It can remove or
insert from front & back easily (const time). and can work as circular
buffer also. in that case it removes from tail automatically when
memory gets exhosted (a cyclic kind of structure). It has the facility
like one can ensure additional capacity for the cyclic buffer
temporarily before a series of push_back. Or one can simply use it as a
deque with an reserve memory and increment factor.
here the code to use it as deque
CircularBuffer<intcb(2,2);//size 2, increment 2
cb.use_as_circular_buffer(false);
cb.push_back(0);
cb.push_back(1);

cout<<"size "<<cb.size() << " cap " <<cb.capacity()<<endl;
typedef CircularBuffer<int>::iterator VecIterator;
for(VecIterator it = cb.begin() ; it!= cb.end(); ++it){
cout<<*it <<" ";
}
cout<<endl;
cb.push_back(2);
cout<<"size "<<cb.size() << " cap " <<cb.capacity()<<endl;
for(VecIterator it = cb.begin() ; it!= cb.end(); ++it){
cout<<*it <<" ";
}
cout<<endl;
cb.push_back(3);
cout<<"size "<<cb.size() << " cap " <<cb.capacity()<<endl;
for(VecIterator it = cb.begin() ; it!= cb.end(); ++it){
cout<<*it <<" ";
}
size 2 cap 2
0 1
size 3 cap 4
0 1 2
size 4 cap 4
0 1 2 3
The use as circular buffer,
comment the line, cb.use_as_circular_buffer(false);
The output is like,
size 2 cap 2
0 1
size 2 cap 2
1 2
size 2 cap 2
2 3
Press ENTER to continue.
And marking works as usual for circular buffer.
Thus the code gives the output,
CircularBuffer<intcb(3,2);
//cb.use_as_circular_buffer(false);
cb.push_back(0);
cb.push_back(1);

cout<<"size "<<cb.size() << " cap " <<cb.capacity()<<endl;
typedef CircularBuffer<int>::iterator VecIterator;
for(VecIterator it = cb.begin() ; it!= cb.end(); ++it){
cout<<*it <<" ";
}
cout<<endl;
VecIterator beg = cb.end()-1;//marked one past. end ponts one more
cb.push_back(2);
cb.push_back(3);
VecIterator end = cb.end();

cout<<"size "<<cb.size() << " cap " <<cb.capacity()<<endl;
for(VecIterator it = beg ; it!= end; ++it){
cout<<*it <<" ";
}
cout<<endl;

cout<<"size "<<cb.size() << " cap " <<cb.capacity()<<endl;
for(VecIterator it = cb.begin() ; it!= cb.end(); ++it){
cout<<*it <<" ";
}
size 2 cap 3
0 1
size 3 cap 3
2 3 =marked data!
size 3 cap 3
1 2 3 =>all data.

Ofcourse, here also if the buffer gets recycled, the marked data will
give new data within that region (as the old data may get partially or
fully removed).

Sep 11 '06 #3
"toton" <ab*******@gmail.comwrites:
Thanks for the reply. with reserve it works fine. However the main
reason is that iterator do not keep the present index in the vector.
rather it keeps a pointer to the present location. Thus when capacity
increases it fails to work. Is it a standard behavior?
Yes. Each container has it's own iterator invalidation
semantics. std::list almost never invalidates, std::vector is quite
fragile. std::string sometimes even invalidates iterators on non-const
calls to begin/end, operator[], etc. due to copy on write.

If you have a nonstandard container it's up to this container to
define the semantics. So I can't tell you what these semantics are.

Jens
Sep 11 '06 #4

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

Similar topics

16
by: Torsten Mueller | last post by:
I have to port a program from Windows to HPUX. It has an algorithm iterating over files of a directory and all of it's subdirectories using _findfirst() and _findnext() on a structure called...
1
by: Serge Poirier | last post by:
Good Day Folks, I'm displaying a date field from an Oracle table in a datagrid with the following template. <ItemTemplate> <asp:Label id="hiredate" runat="server" Text='<%# DataBinder.Eval...
2
by: James Doran | last post by:
Hello, I'd like to iterate through each Page of my ASP.NET project from within a Custom web control and access the Page.Controls collection. I've tried using Reflection on the web project...
7
by: Rich | last post by:
Hello, I have a form with 5 textboxes named txt0, txt1, txt2, txt3, tx4. In VB6 I could iterate through these with For i = 0 to 4 debug.print Me.controls("txt" & i).Name Next
4
by: romy | last post by:
Hi In VB.net I have a set of linkButtons controls in a form , which I want to iterate on them and change their text property. How it's done ? thanks
2
by: John Kotuby | last post by:
Hi all, Again running into problems converting existing ASP to ASP.NET 2 in VB. I am simply trying to print a portion of the HTML displayed on-screen and not the entire document. Specifically I...
15
RMWChaos
by: RMWChaos | last post by:
As usual, an overly-long, overly-explanatory post. Better too much info than too little, right? A couple weeks ago, I asked for some assistance iterating through a JSON property list so that my...
2
by: Luqman | last post by:
I have a Ajax Tab Container on my Webform with One Panel and some Labels and Textboxes. I want to Iterate through all the textboxes and return their IDs. I tried every possibility I could but...
5
by: ray | last post by:
A container object provides a method that returns an iterator object. I need to iterate the sequence with that iterator, but need to skip the first item. I can only iterate the whole sequence with:...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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,...
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.