Standard C++ Library Class Reference
Description
The inplace_merge algorithm merges two sorted consecutive ranges [first, middle) and [middle,
last), and puts the result of the merge into the range [first, last). The merge is stable, that is, 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 (operator<) as the default for comparison, and the second version accepts a third
argument that specifies a comparison operator.
Complexity
When enough additional memory is available, inplace_merge does at most (last - first) - 1
comparisons. If no additional memory is available, an algorithm with O(NlogN) complexity
(where N is equal to last-first) may be used.
Example
//
// merge.cpp
//
#include <algorithm>
#include <vector>
#include <iostream.h>
int main()
{
int d1[4] = {1,2,3,4};
int d2[8] = {11,13,15,17,12,14,16,18};
// Set up two vectors
vector<int> v1(d1,d1 + 4), v2(d1,d1 + 4);
// Set up four destination vectors
vector<int> v3(d2,d2 + 8),v4(d2,d2 + 8),
v5(d2,d2 + 8),v6(d2,d2 + 8);
// Set up one empty vector
vector<int> v7;
// Merge v1 with v2
merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
// Now use comparator
merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v4.begin(),
less<int>());
// In place merge v5
vector<int>::iterator mid = v5.begin();
advance(mid,4);