On Sep 19, 2:13*pm, "pers...@googlemail.com" <pers...@gmail.com>
wrote:
> Hello,
> * * * * *I want to take subsequence from a vector and store it in a
> different vector. I would like to avoid copying. Is there something
> available in boost or stl.
>
> Say,
> vector<int> v(0,1,2,3,4,5,6,7,8,9,10);
>
> I want vector<int> slice to hold 5,6,7,8.
>
> I should be able to do:
>
> Is there a wrapper class like this -
>
> template<class T>
> struct SliceOfVector
> {
> * * * SliceOfVector(pair< vector<T>::iterator , vector<T>::iterator> ) *;
>
> pair< vector<T>::iterator , vector<T>::iterator *> *m_range;
> vector<T>& m_original;
>
> };
>
> SliceOfVector * should have all semantics same as vector. May be even
> derives from vector<T>.
>
> I should be able to treat SliceOfVector *just like a vector, just that
> it would be the subsequence specified by m_range of the original
> vector.
>
> Am I making sense. Please let me know if you have any suggestions.
Not sure about making sense. If slice IS-A vector, what does it mean
WRT vector modifiers? If you insert value in the slice, it's inserted
in the vector? (same for removals and element modification) Or you
don't want modifiers (not a vector then)? Or...?
If your vector is const while your slice is alive, then first/last
iterator pair is just fine. You only get random access, and that
"const" might prove to be a big requirement.
If not, if you want something richer, look into wrapping vector into
class(es) that supports slicing as you want it.
Finally, did you make __any__ measurement on actual use-case, and
taking into consideration the rest of the processing? If not, that "I
want to avoid copying" part smacks of premature optimization

.
Goran.