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
|