473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

splitting a vector into two parts

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<inta = 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.
Jan 18 '07 #1
5 5297
On Jan 18, 9:50 am, "AG" <a...@tb.frwrote:
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

Jan 18 '07 #2
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.

Jan 18 '07 #3

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<inta = 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).

Jan 18 '07 #4
AG
What about using val_array/slice_array ?
Jan 18 '07 #5
On Jan 18, 3:55 pm, "AG" <a...@tb.frwrote:
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

Jan 18 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sandman | last post by:
I am splitting a text block into paragraphs, to be able to add images and stuff like that to a specific paragraph in a content management system. Well, right now I'm splittin on two or more...
3
by: Aaron Walker | last post by:
I have a feeling this going to end up being something so stupid, but right now I'm confused as hell. I'm trying to code a function, that given a string and a delimiter char, returns a vector of...
12
by: Simon | last post by:
Well, the title's pretty descriptive; how would I be able to take a line of input like this: getline(cin,mostrecentline); And split into an (flexible) array of strings. For example: "do this...
2
by: Matt | last post by:
Hi, I'm ridiculously new to Access (about a week!) so please be patient! My database is a record of British Standards. Each has a unique identifier. Some are split into parts. I would like...
2
by: CharChabil | last post by:
Using Vb.net 2005, I want to read each part in this string in an array (splitting the string) ----------- A1/EXT "BK82 LB73 21233" 105 061018 1804 ----------- That Code that i used is as follow:...
13
by: Pedro Pinto | last post by:
Hi there. I'm trying to do the following. I have a string, and i want to separate it into other halves. This is how it should be: char string = "test//test2//test3"; were // is the part...
3
by: jb | last post by:
Am using the 'Web Application Project' model for an asp.net web app. Sections of this now need to be reusable. Is it possible to split into sub projects? What happens with /bin directory if so -...
4
by: yogi_bear_79 | last post by:
I have a simple string (i.e. February 27, 2008) that I need to split into three parts. The month, day, and year. Splitting into a string array would work, and I could convert day and years to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.