combining two stl vectors



 DEVELOP > c-Plus-Plus > combining two stl vectors

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "vasileios zografos"
Date: 20 Dec 2003 03:45:35 PM
Object: combining two stl vectors
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.
.

User: "Ron Natalie"

Title: Re: combining two stl vectors 20 Dec 2003 03:50:41 PM
"vasileios zografos" <restricted@nowhere.net> wrote in message
news:bs2ftv$2tm$1@newsg4.svr.pol.co.uk...

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.
.
User: "vasileios zografos"

Title: Re: combining two stl vectors 20 Dec 2003 03:54:03 PM
Thank you
.


User: "Jeff Schwab"

Title: Re: combining two stl vectors 20 Dec 2003 04:07:52 PM
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 );
}
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER