T
toton
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<int> v;
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.
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<int> v;
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.