merge()

Combines two sorted sequences into a single sorted sequence addressed by the fifth iterator. An optional sixth argument allows us to indicate an ordering other than the default less-than operator.

#include <algorithm> 
int ia[12] =  {29,23,20,22,17,15,26,51,19,12,35,40}; 
int ia2[12] = {74,16,39,54,21,44,62,10,27,41,65,71}; 

vector<int> vec1( ia, ia+12 ),  vec2( ia2, ia2+12 ); 
vector<int> vec_result(vec1.size()+vec2.size()); 

sort( vec1.begin(), vec1.end(), greater<int>() ); 
sort( vec2.begin(), vec2.end(), greater<int>() ); 

merge( vec1.begin(), vec1.end(), 
       vec2.begin(), vec2.end(), 
       vec_result.begin(), greater<int>() ); 

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.