.\" ident @(#)inplace_merge.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH inplace_merge 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2inplace_merge\fP \ - Merges two sorted sequences into one. .SH SYNOPSIS .br #include .br template .RE .RS 1 void inplace_merge(BidirectionalIterator first, .RE .RS 20 BidirectionalIterator middle, .br BidirectionalIterator last); .RE .RS 0 .br template .RE .RS 1 void inplace_merge(BidirectionalIterator first, .RE .RS 20 BidirectionalIterator middle, .br BidirectionalIterator last, .br Compare comp); .SH DESCRIPTION The inplace_merge algorithm merges two sorted consecutive ranges \f2[first, middle)\fP and \f2[middle, last)\fP, and puts the result of the merge into the range \f2[first, last)\fP. The merge is stable, which means that if the two ranges contain equivalent elements, the elements from the first range always precede the elements from the second. There are two versions of the inplace_merge algorithm. The first version uses the less than operator (\f2operator<\fP) as the default for comparison, and the second version accepts a third argument that specifies a comparison operator. .SH COMPLEXITY When enough additional memory is available, inplace_merge does at most\f2 (last - first) - 1\fP comparisons. If no additional memory is available, an algorithm with \f2O(NlogN)\fP complexity (where \f2N\fP is equal to \f2last-first\fP) may be used. .SH EXAMPLE .RE .RS 0 // .br // merge.cpp .br // .RE .RS 1 #include .br #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 0 .br .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 3 // Merge v1 with v2 .RE .RS 2 merge(v1.begin(),v1.end(),v2.begin(),v2.end(), .RE .RS 8 v3.begin()); .RE .RS 3 // Now use comparator .RE .RS 2 merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v4.begin(), .RE .RS 8 less()); .RE .RS 3 // In place merge v5 .RE .RS 2 vector::iterator mid = v5.begin(); .br advance(mid,4); .RE .RS 3 inplace_merge(v5.begin(),mid,v5.end()); .br // Now use a comparator on v6 .RE .RS 2 mid = v6.begin(); .br advance(mid,4); .RE .RS 3 inplace_merge(v6.begin(),mid,v6.end(),less()); .br // Merge v1 and v2 to empty vector using insert iterator .RE .RS 2 merge(v1.begin(),v1.end(),v2.begin(),v2.end(), .RE .RS 8 back_inserter(v7)); .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 .RE .RS 2 merge(v1.begin(),v1.end(),v2.begin(),v2.end(), .RE .RS 8 ostream_iterator(cout," ")); .RE .RS 2 cout << endl; .br return 0; .RE .RS 1 } .RE .RS 0 .br .RE .RS 0 Program Output .br .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 merge