combining two stl vectors

V

vasileios zografos

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.
 
R

Ron Natalie

vasileios zografos said:
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.
 
J

Jeff Schwab

vasileios said:
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 );
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top