std::vector initialization

Discussion in 'C++' started by buchtak@gmail.com, Mar 4, 2009.

  1. Guest

    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.
    , Mar 4, 2009
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    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).
    Kai-Uwe Bux, Mar 4, 2009
    #2
    1. Advertising

  3. Guest

    > 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.
    , Mar 4, 2009
    #3
  4. Guest

    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 :).
    , Mar 4, 2009
    #4
  5. James Kanze Guest

    On Mar 4, 8:37 pm, Jeff Schwab <> wrote:
    > 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:
    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
    James Kanze, Mar 5, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,305
    Ivan Vecerina
    Mar 17, 2005
  2. Anonymous
    Replies:
    20
    Views:
    4,295
    Pete Becker
    Mar 30, 2005
  3. Jason Heyes
    Replies:
    8
    Views:
    725
    Andrew Koenig
    Jan 15, 2006
  4. Replies:
    8
    Views:
    1,916
    Csaba
    Feb 18, 2006
  5. Rune Allnor
    Replies:
    4
    Views:
    946
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page