combining two stl vectors

Discussion in 'C++' started by vasileios zografos, Dec 20, 2003.

  1. Hi,

    I am trying to combine two stl vectors into one

    so something like

    std::vector<int> vector1;
    std::vector<int> vector2;
    std::vector<int> FullVector;


    so that FullVector = vector1 + vector2


    Is there any other better way than using a for loop?

    V.Z.
     
    vasileios zografos, Dec 20, 2003
    #1
    1. Advertising

  2. vasileios zografos

    Ron Natalie Guest

    "vasileios zografos" <> wrote in message
    news:bs2ftv$2tm$...
    > Hi,
    >
    > I am trying to combine two stl vectors into one
    >
    > so something like
    >
    > std::vector<int> vector1;
    > std::vector<int> vector2;
    > std::vector<int> FullVector;
    >
    >
    > so that FullVector = vector1 + vector2
    >

    FullVector.reserve(vector1.size() + vector2.size()); // Optional, tiny
    performance improvement

    FullVector.insert(FullVector.end(), vector1.begin(), vector1.end()); //
    insert the first vector
    FullVector.insert(FullVector.end(), vector2.begin(), vector2.end()); //
    insert the second.
     
    Ron Natalie, Dec 20, 2003
    #2
    1. Advertising

  3. Thank you
     
    vasileios zografos, Dec 20, 2003
    #3
  4. vasileios zografos

    Jeff Schwab Guest

    vasileios zografos wrote:
    > Hi,
    >
    > I am trying to combine two stl vectors into one
    >
    > so something like
    >
    > std::vector<int> vector1;
    > std::vector<int> vector2;
    > std::vector<int> FullVector;
    >
    >
    > so that FullVector = vector1 + vector2
    >
    >
    > Is there any other better way than using a for loop?
    >
    > V.Z.
    >




    #include <vector>
    #include <iterator>

    int main( )
    {
    typedef std::vector< int > V;

    V vector1;
    V vector2;
    V fullvector;

    std::back_insert_iterator< V > p( fullvector );

    copy( vector1.begin( ), vector1.end( ), p );
    copy( vector2.begin( ), vector2.end( ), p );
    }
     
    Jeff Schwab, Dec 20, 2003
    #4
    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. berthelot samuel

    vectors stl with pointers

    berthelot samuel, Jul 30, 2003, in forum: C++
    Replies:
    3
    Views:
    472
    John Harrison
    Jul 30, 2003
  2. Alan

    combining two vectors

    Alan, Nov 14, 2006, in forum: C++
    Replies:
    11
    Views:
    635
    Alf P. Steinbach
    Nov 15, 2006
  3. Chris Roth

    Adding two STL vectors

    Chris Roth, Feb 23, 2007, in forum: C++
    Replies:
    11
    Views:
    867
    Kai-Uwe Bux
    Feb 23, 2007
  4. Replies:
    3
    Views:
    703
    Shadowman
    Mar 26, 2008
  5. Guest
    Replies:
    0
    Views:
    450
    Guest
    Sep 14, 2005
Loading...

Share This Page