splitting a vector into two parts

A

AG

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.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
 
A

AG

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.
 
O

Ondra Holub

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).
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top