473,750 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::it erator mid = a.begin();
mid+=5;

vector<int& a1 = vector<int>(a.b egin(),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.begi n();mid!=a1.end ();mid++)
cout << *mid << " ";
cout << "\na2 ";
for(mid=a2.begi n();mid!=a2.end ();mid++)
cout << *mid << " ";

Would someone know a way to do this ?

many thanks in advance,

Alexandre.
Jan 18 '07 #1
5 5322
On Jan 18, 9:50 am, "AG" <a...@tb.frwrot e:
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>::it erator mid = a.begin();
mid+=5;

vector<int& a1 = vector<int>(a.b egin(),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.begi n();mid!=a1.end ();mid++)
cout << *mid << " ";
cout << "\na2 ";
for(mid=a2.begi n();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.frwrot e:
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
3304
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 newlines, so this text block: Hello, my nickname is Sandman and I am coding some PHP Call me
3
1670
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 the sub-strings. Here's what I have (I've thrown a main() in there for this mail). --- #include <iostream> #include <string>
12
4456
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 action" would go to: item 0: do
2
2875
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 the user to be able to input the data into a single textbox using a form like this: Record1: BS 1490-1 Record2: BS 1490-2
2
1490
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: Dim s As String, h As String Dim delim(1) As Char delim(0) = "/"
13
1986
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 were i want to separate it and store on a
3
1966
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 - are there bins in each subproject? What happens with sessions? Thanks JB
4
2824
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 integers later. I've bene looking around, and everything I see seems more complicated than it should be! Help!
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.