C++ STL algorithm set_union() member program sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

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++ STL set_union() to unite all of the elements that belong to at least one of two sorted source ranges into a single, sorted destination range, where the ordering criterion may be specified by a binary predicate

To show: How to use the C++ algorithm, set_union() to unite all of the elements that belong to at least one of two sorted source ranges into a single, sorted destination range, where the ordering criterion may be specified by a binary predicate in C++ programming

 

 

 

// C++ STL algorithm set_union()

#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 containers

vector<int> vec1a, vec1b, vec1(15);

// vector iterators

vector<int>::iterator Iter1a, Iter1b, Iter1, Result1;

 

// constructing vectors vec1a & vec1b with default less than ordering

int i, j;

for(i = -3; i <= 3; i++)

vec1a.push_back(i);

for(j =-7; j <= 7; j++)

vec1b.push_back(j);

 

cout<<"Original vector vec1a with range sorted by the binary predicate less than is: ";

for(Iter1a = vec1a.begin(); Iter1a != vec1a.end(); Iter1a++)

cout<<*Iter1a<<" ";

cout<<endl;

cout<<"\nOriginal vector vec1b with range sorted by the binary predicate less than is: ";

for(Iter1b = vec1b.begin(); Iter1b != vec1b.end(); Iter1b++)

cout<<*Iter1b<<" ";

cout<<endl;

// constructing vectors vec2a & vec2b with ranges sorted by greater

vector <int>vec2a(vec1a), vec2b(vec1b), vec2(vec1);

vector <int>::iterator Iter2a, Iter2b, Iter2, Result2;

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

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

cout<<"\nOriginal vector vec2a with range sorted by the binary predicate greater is: ";

for(Iter2a = vec2a.begin(); Iter2a != vec2a.end(); Iter2a++)

cout<<*Iter2a<<" ";

cout<<endl;

cout<<"\nOriginal vector vec2b with range sorted by the binary predicate greater is: ";

for(Iter2b = vec2b.begin(); Iter2b != vec2b.end(); Iter2b++)

cout<<*Iter2b<<" ";

cout<<endl;

// constructing vectors vec3a & vec3b with ranges sorted by mod_lesser()

vector <int>vec3a(vec1a), vec3b(vec1b), vec3(vec1);

vector <int>::iterator Iter3a, Iter3b, Iter3, Result3;

sort(vec3a.begin(), vec3a.end(), mod_lesser);

sort(vec3b.begin(), vec3b.end(), mod_lesser);

cout<<"\nOriginal vector vec3a with range sorted by the binary predicate mod_lesser() is: ";

for(Iter3a = vec3a.begin(); Iter3a != vec3a.end(); Iter3a++)

cout<<*Iter3a<<" ";

cout<<endl;

cout<<"\nOriginal vector vec3b with range sorted by the binary predicate mod_lesser() is: ";

for(Iter3b = vec3b.begin(); Iter3b != vec3b.end(); Iter3b++)

cout<<*Iter3b<<" ";

cout<<endl;

// to combine into a union in ascending order with the default binary predicate less <int>()

Result1 = set_union(vec1a.begin(), vec1a.end(), vec1b.begin(), vec1b.end(), vec1.begin());

cout<<"\nset_union() of source ranges with default order, vec1mod vector: ";

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

cout<<*Iter1<<" ";

cout<<endl;

// to combine into a union in descending order, specify binary predicate greater<int>()

Result2 = set_union(vec2a.begin(), vec2a.end(), vec2b.begin(), vec2b.end(), vec2.begin(), greater<int>());

cout<<"\nset_union() of source ranges with binary predicate greater specified, vec2mod vector: ";

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

cout<<*Iter2<<" ";

cout<<endl;

// to combine into a union applying a user-defined binary predicate mod_lesser

Result3 = set_union(vec3a.begin(), vec3a.end(), vec3b.begin(), vec3b.end(), vec3.begin(), mod_lesser);

cout<<"\nset_union() of source ranges with binary predicate mod_lesser() specified, vec3mod vector: ";

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

cout<<*Iter3<<" ";

cout<<endl;

return 0;

}

 

Output example:

 

Original vector vec1a with range sorted by the binary predicate less than is: -3 -2 -1 0 1 2 3

Original vector vec1b with range sorted by the binary predicate less than is: -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7

Original vector vec2a with range sorted by the binary predicate greater is: 3 2 1 0 -1 -2 -3

Original vector vec2b with range sorted by the binary predicate greater is: 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7

Original vector vec3a with range sorted by the binary predicate mod_lesser() is: 0 -1 1 -2 2 -3 3

Original vector vec3b with range sorted by the binary predicate mod_lesser() is: 0 -1 1 -2 2 -3 3 -4 4 -5 5 -6 6 -7 7

set_union() of source ranges with default order, vec1mod vector: -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7

set_union() of source ranges with binary predicate greater specified, vec2mod vector: 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7

set_union() of source ranges with binary predicate mod_lesser() specified, vec3mod vector: 0 -1 1 -2 2 -3 3 -4 4 -5 5 -6 6 -7 7

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index