Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > getting a slice off a vector

Reply
Thread Tools

getting a slice off a vector

 
 
persres@googlemail.com
Guest
Posts: n/a
 
      09-19-2011
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.


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-19-2011
On 9/19/2011 8:13 AM, 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.


Storing iterators is dangerous. Storing std::vector iterators is
dangerous squared. Basically you need to make sure that your vector
does not reallocate its memory because that action invalidates all
iterators, pointers and references to vector's elements.

I recommend storing indices. In the debug version of your program I'd
actually check against the size of 'm_original' every time the vector is
accessed using the index.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Jeff Flinn
Guest
Posts: n/a
 
      09-19-2011
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.


That's a range or sub-range not a slice. IIRC, a slice would be more
akin to a row or column of a 2d matrix, or a 2d matrix from a 3d array.

Have you looked at:
http://www.boost.org/doc/libs/1_47_0...tml/index.html

It differs from your implementation, in that both iterator_range and
it's derivate, sub_range hold iterators. While your implementation holds
a vector ref. The boost range holding vector iterators would be
invalidated if the underlying vector invalidates those iterators.

Jeff
 
Reply With Quote
 
persres@googlemail.com
Guest
Posts: n/a
 
      09-19-2011
Yes, true about iterators. Methinks its worth having a light weight
make_range function that will live for a short time. I can't see how
it could be done with the existing std::vector.
Thanks
 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      09-19-2011
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.
 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      09-19-2011
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
how do i create a vector within a vector ? learningjava Java 5 10-17-2003 10:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57