Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > splitting a vector into two parts

Reply
Thread Tools

splitting a vector into two parts

 
 
AG
Guest
Posts: n/a
 
      01-18-2007
Hi,

I have a vector that represent memory in my code. I would like to split it into two smaller vector, without
copying it. I want the split to be "in-place", so that modifications on the two smaller vectors would affect the
original one.

Exemple :

int N=10;
int i;
vector<int> a = vector<int>(N,0);
for(i=0;i<N;i++)
a.push_back(i);

vector<int>::iterator mid = a.begin();
mid+=5;

vector<int> & a1 = vector<int>(a.begin(),mid); // something like this
vector<int> & a2 = vector<int>(mid,a.end()); // something like this

a1[0] = 10;
a2[0] = 22;

cout << "a ";
for(mid=a.begin();mid!=a.end();mid++)
cout << *mid << " ";
cout << "\na1 ";
for(mid=a1.begin();mid!=a1.end();mid++)
cout << *mid << " ";
cout << "\na2 ";
for(mid=a2.begin();mid!=a2.end();mid++)
cout << *mid << " ";

Would someone know a way to do this ?

many thanks in advance,

Alexandre.


 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-18-2007
On Jan 18, 9:50 am, "AG" <a...@tb.fr> wrote:
> Hi,
>
> I have a vector that represent memory in my code. I would like to split it into two smaller vector, without
> copying it. I want the split to be "in-place", so that modifications on the two smaller vectors would affect the
> original one.


You can't do that with a vector, each vector handles the objects it
contains. What if (in your example) someone were to do something like:

a1[0] = 5;
a[0] = 1;

Then, suddenly, a1[0] == 1, which is not logical in any way, since you
have not changed a1.

What you could do is to either use normal arrays:

int* a = new int[10];
int* a1 = a;
int* a2 = a[5];

Just make sure that you don't delete a before you are done with a1 and
a2, and don't delete either a1 or a2 (deleting a1 is the same as
deleting a, and deleting a2 will probably throw an exception at the
very least).

Or you can make a wrapper-class that works much like a vector and takes
two random-access iterators as constructors (start and end iterators).

By the way, if it's to represent memory you might want to use char
instead of int, since that allows your to access each byte
individually.

--
Erik Wikström

 
Reply With Quote
 
 
 
 
AG
Guest
Posts: n/a
 
      01-18-2007
>You can't do that with a vector, each vector handles the objects it
>contains. What if (in your example) someone were to do something
>like:
>
>a1[0] = 5;
>a[0] = 1;
>
>Then, suddenly, a1[0] == 1, which is not logical in any way, since
>you
>have not changed a1.


This is exactly the behavior I would expect, and that is also what you
get when doing :

int a = 5;
int &b = a;

b = 1;

Then, suddenly, a == 1, which IS logical.



>What you could do is to either use normal arrays:


>int* a = new int[10];
>int* a1 = a;
>int* a2 = a[5];


That's what I was used to do, but moving to vectors... I would have
expected the same behavior possible. So bad.



 
Reply With Quote
 
Ondra Holub
Guest
Posts: n/a
 
      01-18-2007

AG napsal:
> Hi,
>
> I have a vector that represent memory in my code. I would like to split it into two smaller vector, without
> copying it. I want the split to be "in-place", so that modifications on the two smaller vectors would affect the
> original one.
>
> Exemple :
>
> int N=10;
> int i;
> vector<int> a = vector<int>(N,0);
> for(i=0;i<N;i++)
> a.push_back(i);
>
> vector<int>::iterator mid = a.begin();
> mid+=5;
>
> vector<int> & a1 = vector<int>(a.begin(),mid); // something like this
> vector<int> & a2 = vector<int>(mid,a.end()); // something like this
>
> a1[0] = 10;
> a2[0] = 22;
>
> cout << "a ";
> for(mid=a.begin();mid!=a.end();mid++)
> cout << *mid << " ";
> cout << "\na1 ";
> for(mid=a1.begin();mid!=a1.end();mid++)
> cout << *mid << " ";
> cout << "\na2 ";
> for(mid=a2.begin();mid!=a2.end();mid++)
> cout << *mid << " ";
>
> Would someone know a way to do this ?
>
> many thanks in advance,
>
> Alexandre.


Write your own class, which stores reference to original vector and
indexes of begin and end for your subvector (you cannot store
iterators, because iterators may become invalid when size of vector is
increased).

 
Reply With Quote
 
AG
Guest
Posts: n/a
 
      01-18-2007
What about using val_array/slice_array ?


 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-18-2007
On Jan 18, 3:55 pm, "AG" <a...@tb.fr> wrote:
> What about using val_array/slice_array ?


You could use a valarray and slice it up yes. It does, however, not
dynamically grow as a vector would (and I assume that's why you use a
vector instead of a normal array), but it can be resized. Don't know
how slices work when resizing tough.

--
Erik Wikström

 
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
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
Parts parts....PARTS!!! ARGHHH dstvns A+ Certification 8 01-07-2004 07:57 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 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