combining two vectors

Discussion in 'C++' started by Alan, Nov 14, 2006.

  1. Alan

    Alan Guest

    Is there an easy and efficient way to combine two <vector>s, rather
    than taking each element from one and adding it to the other?

    I haven`t been able to find any guidance on or examples of this
    sort of operation.

    Thanks, Alan
    Alan, Nov 14, 2006
    #1
    1. Advertising

  2. Alan

    Ian Collins Guest

    Alan wrote:
    > Is there an easy and efficient way to combine two <vector>s, rather
    > than taking each element from one and adding it to the other?
    >
    > I haven`t been able to find any guidance on or examples of this
    > sort of operation.
    >


    vector1.insert( vector1.end(), vector2.begin(), vector2.end() );

    --
    Ian Collins.
    Ian Collins, Nov 14, 2006
    #2
    1. Advertising

  3. Alan

    Alan Guest

    Ian, Thanks! Alan
    Alan, Nov 14, 2006
    #3
  4. Alan

    Stefan Naewe Guest

    Ian Collins schrieb:
    > Alan wrote:
    >> Is there an easy and efficient way to combine two <vector>s, rather
    >> than taking each element from one and adding it to the other?
    >>
    >> I haven`t been able to find any guidance on or examples of this
    >> sort of operation.
    >>

    >
    > vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
    >


    It's easy - yes.
    But is it really efficient ?
    Isn't it as unefficient as repeatedly calling push_bask() on vector1 ?

    /S
    --
    Stefan Naewe
    stefan_DOT_naewe_AT_atlas_DOT_de
    Stefan Naewe, Nov 15, 2006
    #4
  5. Alan

    Pete Becker Guest

    Stefan Naewe wrote:
    > Ian Collins schrieb:
    >> Alan wrote:
    >>> Is there an easy and efficient way to combine two <vector>s, rather
    >>> than taking each element from one and adding it to the other?
    >>>
    >>> I haven`t been able to find any guidance on or examples of this
    >>> sort of operation.
    >>>

    >> vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
    >>

    >
    > It's easy - yes.
    > But is it really efficient ?
    > Isn't it as unefficient as repeatedly calling push_bask() on vector1 ?
    >


    No. The iterators that represent the range being copied are random
    access iterators, so insert can figure out how many elements will be
    added and adjust the size accordingly. Repeatedly calling push_back
    could end up reallocating the storage space more than once.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Nov 15, 2006
    #5
  6. Alan

    Stefan Naewe Guest

    Pete Becker schrieb:
    > Stefan Naewe wrote:
    >> Ian Collins schrieb:
    >>> Alan wrote:
    >>>> Is there an easy and efficient way to combine two <vector>s, rather
    >>>> than taking each element from one and adding it to the other?
    >>>>
    >>>> I haven`t been able to find any guidance on or examples of this
    >>>> sort of operation.
    >>>>
    >>> vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
    >>>

    >>
    >> It's easy - yes.
    >> But is it really efficient ?
    >> Isn't it as unefficient as repeatedly calling push_bask() on vector1 ?
    >>

    >
    > No. The iterators that represent the range being copied are random
    > access iterators, so insert can figure out how many elements will be
    > added and adjust the size accordingly. Repeatedly calling push_back
    > could end up reallocating the storage space more than once.


    OK.
    But what if I do a vector1.reserve(x) (with a valid x...) before ?
    'vector2.end()-vector2.begin()' elements need to be copied after vector1.end(), right?

    (Using std::list and std::list::splice() would be more efficient in this case, wouldn't it?)


    /S
    --
    Stefan Naewe
    stefan_DOT_naewe_AT_atlas_DOT_de
    Stefan Naewe, Nov 15, 2006
    #6
  7. Alan

    red floyd Guest

    Stefan Naewe wrote:
    > Ian Collins schrieb:
    >> Alan wrote:
    >>> Is there an easy and efficient way to combine two <vector>s, rather
    >>> than taking each element from one and adding it to the other?
    >>>
    >>> I haven`t been able to find any guidance on or examples of this
    >>> sort of operation.
    >>>

    >> vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
    >>

    >
    > It's easy - yes.
    > But is it really efficient ?
    > Isn't it as unefficient as repeatedly calling push_bask() on vector1 ?


    Why does everyone ask "is this more efficient" in a vacuum?

    Have you benchmarked to determine that your vector operations are the
    bottleneck? An "efficient" program that doesn't work right is not as
    good as an "inefficient" program that does.

    Hoare's Law (also attributed to Knuth): "Premature optimization is the
    root of all evil".
    red floyd, Nov 15, 2006
    #7
  8. Alan

    Pete Becker Guest

    red floyd wrote:
    > Stefan Naewe wrote:
    >> Ian Collins schrieb:
    >>> Alan wrote:
    >>>> Is there an easy and efficient way to combine two <vector>s, rather
    >>>> than taking each element from one and adding it to the other?
    >>>>
    >>>> I haven`t been able to find any guidance on or examples of this
    >>>> sort of operation.
    >>>>
    >>> vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
    >>>

    >>
    >> It's easy - yes.
    >> But is it really efficient ?
    >> Isn't it as unefficient as repeatedly calling push_bask() on vector1 ?

    >
    > Why does everyone ask "is this more efficient" in a vacuum?
    >
    > Have you benchmarked to determine that your vector operations are the
    > bottleneck? An "efficient" program that doesn't work right is not as
    > good as an "inefficient" program that does.
    >


    And how is changing from push_back to insert going to make this code not
    work right?

    > Hoare's Law (also attributed to Knuth): "Premature optimization is the
    > root of all evil".


    Okay, let's all use bubble sort until we can prove that quicksort will
    be a better choice.

    Sometimes optimization without measurement is perfectly appropriate.
    When there's a choice of two ways to do something and one is slower,
    choose the other.

    for (iter = vector2.begin(); iter != vector2.end(); ++iter)
    vector1.push_back(*iter);

    versus

    vector1.insert(vector1.end(), vector2.begin(), vector2.end());

    No contest. Use the latter.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Nov 15, 2006
    #8
  9. Alan

    Ian Collins Guest

    Pete Becker wrote:
    > red floyd wrote:
    >
    >>
    >> Why does everyone ask "is this more efficient" in a vacuum?
    >>
    >> Have you benchmarked to determine that your vector operations are the
    >> bottleneck? An "efficient" program that doesn't work right is not as
    >> good as an "inefficient" program that does.
    >>

    >
    > And how is changing from push_back to insert going to make this code not
    > work right?
    >
    >> Hoare's Law (also attributed to Knuth): "Premature optimization is the
    >> root of all evil".

    >
    >
    > Okay, let's all use bubble sort until we can prove that quicksort will
    > be a better choice.
    >
    > Sometimes optimization without measurement is perfectly appropriate.
    > When there's a choice of two ways to do something and one is slower,
    > choose the other.
    >

    Agreed, maybe the quote should be changed to "Premature
    micro-optimisation is the root of all evil" to differentiate between
    code tweaks and choice of algorithm.

    --
    Ian Collins.
    Ian Collins, Nov 15, 2006
    #9
  10. * Ian Collins:
    > Pete Becker wrote:
    >> red floyd wrote:
    >>
    >>> Hoare's Law (also attributed to Knuth): "Premature optimization is the
    >>> root of all evil".

    >>
    >> Okay, let's all use bubble sort until we can prove that quicksort will
    >> be a better choice.
    >>
    >> Sometimes optimization without measurement is perfectly appropriate.
    >> When there's a choice of two ways to do something and one is slower,
    >> choose the other.
    >>

    > Agreed, maybe the quote should be changed to "Premature
    > micro-optimisation is the root of all evil" to differentiate between
    > code tweaks and choice of algorithm.


    Isn't that an optimization?

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 15, 2006
    #10
  11. Alan

    Ian Collins Guest

    Alf P. Steinbach wrote:
    > * Ian Collins:
    >
    >> Pete Becker wrote:
    >>
    >>> red floyd wrote:
    >>>
    >>>> Hoare's Law (also attributed to Knuth): "Premature optimization is the
    >>>> root of all evil".
    >>>
    >>>
    >>> Okay, let's all use bubble sort until we can prove that quicksort will
    >>> be a better choice.
    >>>
    >>> Sometimes optimization without measurement is perfectly appropriate.
    >>> When there's a choice of two ways to do something and one is slower,
    >>> choose the other.
    >>>

    >> Agreed, maybe the quote should be changed to "Premature
    >> micro-optimisation is the root of all evil" to differentiate between
    >> code tweaks and choice of algorithm.

    >
    >
    > Isn't that an optimization?
    >

    More like a design decision.

    --
    Ian Collins.
    Ian Collins, Nov 15, 2006
    #11
  12. * Ian Collins:
    > Alf P. Steinbach wrote:
    >> * Ian Collins:
    >>
    >>> Pete Becker wrote:
    >>>
    >>>> red floyd wrote:
    >>>>
    >>>>> Hoare's Law (also attributed to Knuth): "Premature optimization is the
    >>>>> root of all evil".
    >>>>
    >>>> Okay, let's all use bubble sort until we can prove that quicksort will
    >>>> be a better choice.
    >>>>
    >>>> Sometimes optimization without measurement is perfectly appropriate.
    >>>> When there's a choice of two ways to do something and one is slower,
    >>>> choose the other.
    >>>>
    >>> Agreed, maybe the quote should be changed to "Premature
    >>> micro-optimisation is the root of all evil" to differentiate between
    >>> code tweaks and choice of algorithm.

    >>
    >> Isn't that an optimization?
    >>

    > More like a design decision.


    Well, yes, but isn't that an optimization? <g>

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 15, 2006
    #12
    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. williams
    Replies:
    1
    Views:
    873
  2. Winterminute

    Combining Two Data Sources for one Data List

    Winterminute, Dec 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    813
    Winterminute
    Dec 3, 2003
  3. vasileios zografos

    combining two stl vectors

    vasileios zografos, Dec 20, 2003, in forum: C++
    Replies:
    3
    Views:
    12,678
    Jeff Schwab
    Dec 20, 2003
  4. Replies:
    3
    Views:
    675
    Shadowman
    Mar 26, 2008
  5. Guest
    Replies:
    0
    Views:
    425
    Guest
    Sep 14, 2005
Loading...

Share This Page