T
toton
Hi,
I am facing problem some times, and not finding a suitable answer to
solve it.
I have a vector (or some other container, sometimes) of some class,
named CC
like vector<CC> v, NOT vector<CC*> ;
I want another class say Segment to refer a portion of this vector. I
can't store the iterator for vector as a member function to the class,
as
1) The iterators have different invalidation strategy, and may
invalidate the stored iterators when some new elements are added to the
vector.
2) The iterators are not default constructible, where I need to
calculate the range after Segment class is formed.
Someone suggested, storing raw index of the position, as size_t, and
construct iterator from that. But that looks little error prone.
Things are much easy if I use vector<shared_ptr<CC> > rather than
vector<CC> , where I can copy a portion of the vector inside Segment,
as it copies only the pointers (Which looks much like Java collections,
list, subList etc ). However, I want the vector to store CC directly
like vector<CC>,
and Segment to use a reference of the portion (like
vector<shared_ptr<CC> > , as vector<CC&> is not possible , or vector
<CC*> also not possible as the elements in a vector are movable and may
invalidate the pointer , or use iterators).
In actual case, lots of different hierarchical views are associated
with the vector<CC> all of them points to a range in the vector.
Hope, the problem statement is understandable. If needed, I will be
able to supply more detail.
Looking for an elegant solution.
Thanks in advance.
abir
I am facing problem some times, and not finding a suitable answer to
solve it.
I have a vector (or some other container, sometimes) of some class,
named CC
like vector<CC> v, NOT vector<CC*> ;
I want another class say Segment to refer a portion of this vector. I
can't store the iterator for vector as a member function to the class,
as
1) The iterators have different invalidation strategy, and may
invalidate the stored iterators when some new elements are added to the
vector.
2) The iterators are not default constructible, where I need to
calculate the range after Segment class is formed.
Someone suggested, storing raw index of the position, as size_t, and
construct iterator from that. But that looks little error prone.
Things are much easy if I use vector<shared_ptr<CC> > rather than
vector<CC> , where I can copy a portion of the vector inside Segment,
as it copies only the pointers (Which looks much like Java collections,
list, subList etc ). However, I want the vector to store CC directly
like vector<CC>,
and Segment to use a reference of the portion (like
vector<shared_ptr<CC> > , as vector<CC&> is not possible , or vector
<CC*> also not possible as the elements in a vector are movable and may
invalidate the pointer , or use iterators).
In actual case, lots of different hierarchical views are associated
with the vector<CC> all of them points to a range in the vector.
Hope, the problem statement is understandable. If needed, I will be
able to supply more detail.
Looking for an elegant solution.
Thanks in advance.
abir