Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::vector initialization

Reply
Thread Tools

std::vector initialization

 
 
buchtak@gmail.com
Guest
Posts: n/a
 
      03-04-2009
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.
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      03-04-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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).
 
Reply With Quote
 
 
 
 
buchtak@gmail.com
Guest
Posts: n/a
 
      03-04-2009
> 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.
 
Reply With Quote
 
buchtak@gmail.com
Guest
Posts: n/a
 
      03-04-2009
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 .

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-05-2009
On Mar 4, 8:37 pm, Jeff Schwab <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > 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?


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

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what's the difference between value-initialization and default-initialization? Jess C++ 23 05-04-2007 03:03 AM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments