std::vector initialization

B

buchtak

Hi,

is there a simple way, how to make a shallow copy of a std::vector
without unnecesary memory allocation?
I have a vector myVec and function, which takes as one of the
arguments reference to a std::vector<int>, something like

std::vector<int> myVec;
....
void foo( std::vector<int> &v ) { ... }.

What i need to do is to process (by foo()) only part of the vector
myVec (for example second half, or some interval). The straightforward
solution may be to make a deep copy of the second half of myVec and
then pass it to the function foo(), or to change the header of the
function foo() to take arguments like int first and int size (or
iterators), but that's unhandy for me. Is there a simple way to
initialize a new vector to point to the same memory as myVec, but
setting _Myfirst member to the half of myVec array, which i could then
pass to the foo()? Or is this kind of solution silly and even worse?
Thx for advice.
 
K

Kai-Uwe Bux

Hi,

is there a simple way, how to make a shallow copy of a std::vector
without unnecesary memory allocation?
I have a vector myVec and function, which takes as one of the
arguments reference to a std::vector<int>, something like

std::vector<int> myVec;
...
void foo( std::vector<int> &v ) { ... }.

What i need to do is to process (by foo()) only part of the vector
myVec (for example second half, or some interval). The straightforward
solution may be to make a deep copy of the second half of myVec and
then pass it to the function foo(), or to change the header of the
function foo() to take arguments like int first and int size (or
iterators), but that's unhandy for me.

I would prefer a signature like this:

template < typename RandomAccessIter >
void foo ( RandomAccessIter from, RandomAccessIter to ) {
}

or if you want to avoid templates, the same for vector<int>::iterator.

In any case, if the straight forward solution is unhandy, you limit your
options to unmaintainable, roundabout ways. I strongly recommend to
reconsider.
Is there a simple way to
initialize a new vector to point to the same memory as myVec, but
setting _Myfirst member

_Myfirst is an undefined term. What do you mean?
to the half of myVec array, which i could then
pass to the foo()? Or is this kind of solution silly and even worse?

a) Two vectors with overlapping memory can be realized through custom
allocator. _Fortunately_ that isn't easy.

b) That it's hard is fortunate, because after the creation of the two
vectors, you will encounter undefined behavior sooner or later. E.g., if
one of the vectors reallocates or is destructed, the objects contained in
the other will be destroyed.
Thx for advice.

Change the signature of foo().


Best

Kai-Uwe Bux
It appears that foo should have a different signature:

template < typename RandomAccessIter >
void foo ( RandomAccessIter from, RandomAccessIter to ) {
}

or something like that (maybe, just for vector<int>::iterator).
 
B

buchtak

E.g., if one of the vectors reallocates or is destructed, the objects contained in
the other will be destroyed.

I guess I did not really thought this solution through. Thank you very
much for quick replies, i'll go with the signature change.
 
B

buchtak

Thx Jeff for elegant STL solution, but is there an advantage of using
function objects you propose over a standard function definition in
this particular case? Btw it really looked like an ancient Greek at
first :).
 
J

James Kanze

Yes. By baking the actual function into a static type, you
get to work with it at compile-time. If you were just to pass
down a raw function (qsort-style), it would decay to a
function-pointer. In this example, all calls by the algorithm
implementation (for_each) to the function would have to be
made through the function-pointer, and performance could
suffer.

But he's not passing it around; he's just calling it. In which
case, making it an object is just excess verbiage.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top