.\" ident @(#)merge.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH merge 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2merge\fP \ - Merges two sorted sequences into a third sequence. .SH SYNOPSIS .br #include .br template .RE .RS 1 OutputIterator .RE .RS 2 merge(InputIterator first1, InputIterator1 last1, .RE .RS 7 InputIterator2 first2, InputIterator last2, .br OutputIterator result); .RE .RS 0 .br template .RE .RS 1 OutputIterator .RE .RS 2 merge(InputIterator1 first1, InputIterator1 last1, .RE .RS 7 InputIterator2 first2, InputIterator last2, .br OutputIterator result, Compare comp); .SH DESCRIPTION The merge algorithm merges two sorted sequences, specified by \f2[first1, last1)\fP and \f2[first2, last2)\fP, into the sequence specified by \f2[result, result + (last1 - first1) + (last2 - first2))\fP. The first version of the merge algorithm uses the less than operator (\f2<\fP) to compare elements in the two sequences. The second version uses the comparison function included in the function call. If a comparison function is included, merge assumes that both sequences were sorted using that comparison function. The merge is stable. This means that if the two original sequences contain equivalent elements, the elements from the first sequence always precede the matching elements from the second in the resulting sequence. The size of the result of a merge is equal to the sum of the sizes of the two argument sequences. merge returns an iterator that points to the end of the resulting sequence (in other words, \f2result + (last1 - first1) + (last2 -first2)\fP). The result of merge is undefined if the resulting range overlaps with either of the original ranges. merge assumes that there are at least \f2(last1 - first1) + (last2 -\fP \f2first2)\fP elements following \f2result\fP, unless \f2result\fP has been adapted by an insert iterator. .SH COMPLEXITY At most \f2(last - first1) + (last2 - first2) - 1\fP comparisons are performed. .SH EXAMPLE .RE .RS 0 // .br // merge.cpp .br // .RE .RS 1 #include .br #include .br #include .RE .RS 0 using namespace std; .br .br int main() .RE .RS 1 { .RE .RS 2 int d1[4] = {1,2,3,4}; .br int d2[8] = {11,13,15,17,12,14,16,18}; .RE .RS 0 .RE .RS 3 // Set up two vectors .RE .RS 2 vector v1(d1,d1 + 4), v2(d1,d1 + 4); .RE .RS 3 // Set up four destination vectors .RE .RS 2 vector v3(d2,d2 + 8),v4(d2,d2 + 8), .RE .RS 14 v5(d2,d2 + 8),v6(d2,d2 + 8); .RE .RS 3 // Set up one empty vector .RE .RS 2 vector v7; .RE .RS 0 .RE .RS 3 // Merge v1 with v2 .br merge(v1.begin(),v1.end(),v2.begin(),v2.end(), .RE .RS 8 v3.begin()); .RE .RS 3 // Now use comparator .br merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v4.begin(), .RE .RS 8 less()); .RE .RS 0 .RE .RS 3 // In place merge v5 .RE .RS 2 vector::iterator mid = v5.begin(); .br advance(mid,4); .br inplace_merge(v5.begin(),mid,v5.end()); .RE .RS 3 // Now use a comparator on v6 .RE .RS 2 mid = v6.begin(); .br advance(mid,4); .br inplace_merge(v6.begin(),mid,v6.end(),less()); .RE .RS 3 .br // Merge v1 and v2 to empty vector using insert iterator .br merge(v1.begin(),v1.end(),v2.begin(),v2.end(), .RE .RS 8 back_inserter(v7)); .RE .RS 0 .RE .RS 3 // Copy all cout .RE .RS 2 ostream_iterator out(cout," "); .br copy(v1.begin(),v1.end(),out); .br cout << endl; .br copy(v2.begin(),v2.end(),out); .br cout << endl; .br copy(v3.begin(),v3.end(),out); .br cout << endl; .br copy(v4.begin(),v4.end(),out); .br cout << endl; .br copy(v5.begin(),v5.end(),out); .br cout << endl; .br copy(v6.begin(),v6.end(),out); .br cout << endl; .br copy(v7.begin(),v7.end(),out); .br cout << endl; .RE .RS 4 .RE .RS 3 // Merge v1 and v2 to cout .br merge(v1.begin(),v1.end(),v2.begin(),v2.end(), .RE .RS 8 ostream_iterator(cout," ")); .RE .RS 2 cout << endl; .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br 1 2 3 4 .br 1 2 3 4 .br 1 1 2 2 3 3 4 4 .br 1 1 2 2 3 3 4 4 .br 11 12 13 14 15 16 17 18 .br 11 12 13 14 15 16 17 18 .br 1 1 2 2 3 3 4 4 .br 1 1 2 2 3 3 4 4 .SH WARNINGS If your compiler does not support default template parameters, then you always need to supply the \f2Allocator\fP template argument. For instance, you have to write: \f2vector >\fP instead of: \f2vector\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO Containers, inplace_merge