By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

combining two stl vectors

P: n/a
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.

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"vasileios zografos" <re********@nowhere.net> wrote in message
news:bs**********@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.
Jul 22 '05 #2

P: n/a
Thank you

Jul 22 '05 #3

P: n/a
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 );
}

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.