C++ STL algorithm, inplace_merge() program example

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ inplace_merge() to combine the elements from two consecutive sorted ranges into a single sorted range, where the ordering criterion may be specified by a binary predicate in C++ programming

To show: How to use the C++ algorithm, inplace_merge() to combine the elements from two consecutive sorted ranges into a single sorted range, where the ordering criterion may be specified by a binary predicate in C++ programming

// C++ STL algorithm, inplace_merge()

#include <vector>

#include <algorithm>

// for greater<int>()

#include <functional>

#include <iostream>

using namespace std;

// return whether modulus of elem1 is less than modulus of elem2

bool mod_lesser(int elem1, int elem2)

{

if(elem1 < 0)

elem1 = - elem1;

if(elem2 < 0)

elem2 = - elem2;

return (elem1 < elem2);

}

int main(void)

{

// vector container

vector <int> vec1;

// vector iterators

vector <int>::iterator Iter1, Iter2, Iter3;

int i, j;

// constructing vector vec1 with default less-than ordering

//

// pushing data

for(i = 0; i <= 5; i++)

vec1.push_back(i);

for(j =-5; j <= 0; j++)

vec1.push_back(j);

// printing data

cout<<"vec1 vector data with subranges sorted by the binary predicate less than is: ";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// constructing vector vec2 with ranges sorted by greater

//

// vector container

vector <int> vec2(vec1);

// vector iterator

vector <int>::iterator break2;

break2 = find(vec2.begin(), vec2.end(), -5);

// do some sorting

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

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

// printing data

cout<<"\nvec2 vector data with subranges sorted by the binary predicate greater is: ";

for(Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)

cout<<*Iter2<<" ";

cout<<endl;

// constructing vector vec3 with ranges sorted by mod_lesser()

//

// vector container

vector <int> vec3(vec1);

// vector iterator

vector <int>::iterator break3;

break3 = find(vec3.begin(), vec3.end(), -5);

// do some sorting

sort(vec3.begin(), break3, mod_lesser);

sort(break3, vec3.end(), mod_lesser);

// printing data

cout<<"\nvec3 vector data with subranges sorted by the binary predicate mod_lesser is: ";

for(Iter3 = vec3.begin(); Iter3 != vec3.end(); Iter3++)

cout<<*Iter3<<" ";

cout<<endl;

// another vector iterator

vector <int>::iterator break1;

break1 = find(vec1.begin(), vec1.end(), -5);

// inplace_merge() operation

inplace_merge(vec1.begin(), break1, vec1.end());

// printing

cout<<"\nvec1merg vector data, inplace_merge() with default order: ";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// to merge inplace in descending order, specify binary predicate greater<int>()

inplace_merge(vec2.begin(), break2, vec2.end(), greater<int>());

cout<<"\nvec2merg vector data, inplace_merge() with binary predicate greater specified: ";

for(Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)

cout<<*Iter2<<" ";

cout<<endl;

// applying a user defined binary predicate mod_lesser()

inplace_merge(vec3.begin(), break3, vec3.end(), mod_lesser);

cout<<"\nvec3merg vector data, merged inplace with binary predicate mod_lesser specified: ";

for(Iter3 = vec3.begin(); Iter3 != vec3.end(); Iter3++)

cout<<*Iter3<<" ";

cout<<endl;

return 0;

}

Output examples:

vec1 vector data with subranges sorted by the binary predicate less than is: 0 1 2 3 4 5 -5 -4 -3 -2 -1 0

vec2 vector data with subranges sorted by the binary predicate greater is: 5 4 3 2 1 0 0 -1 -2 -3 -4 -5

vec3 vector data with subranges sorted by the binary predicate mod_lesser is: 0 1 2 3 4 5 0 -1 -2 -3 -4 -5

vec1merg vector data, inplace_merge() with default order: -5 -4 -3 -2 -1 0 0 1 2 3 4 5

vec2merg vector data, inplace_merge() with binary predicate greater specified: 5 4 3 2 1 0 0 -1 -2 -3 -4 -5

vec3merg vector data, merged inplace with binary predicate mod_lesser specified: 0 0 1 -1 2 -2 3 -3 4 -4 5 -5

Press any key to continue . . .