C++ STL algorithm, set_intersection() 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++ set_intersection() to unite all of the elements that belong to both sorted source ranges into a single, sorted destination range, where the ordering criterion may be specified by a binary predicate in C++ programming

To show: How to use the C++ algorithm, set_intersection() to unite all of the elements that belong to both 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_intersection()

#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(12);

// vector iterators

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

int i, j;

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

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

vec1a.push_back(i);

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

vec1b.push_back(j);

// print the data

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

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

cout<<*Iter1a<<" ";

cout<<endl;

cout<<"\nOriginal vec1b vector 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 vec2a vector with range sorted by the binary predicate greater is: ";

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

cout<<*Iter2a<<" ";

cout<<endl;

cout<<"\nOriginal vec2b vector 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 vec3a vector with range sorted by the binary predicate mod_lesser() is: ";

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

cout<<*Iter3a<<" ";

cout<<endl;

cout<<"\nOriginal vec3b vector 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 an intersection in ascending order with the default binary predicate less <int>()

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

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

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

cout<<*Iter1<<" ";

cout<<endl;

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

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

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

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

cout<<*Iter2<<" ";

cout<<endl;

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

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

cout<<"\nset_intersection() 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 examples:

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

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

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

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

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

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

set_intersection() of source ranges with default order, vec1mod vector: -2 -1 0

set_intersection() of source ranges with binary predicate greater specified, vec2mod vector: 0 -1 -2

set_intersection() of source ranges with binary predicate mod_lesser() specified, vec3mod vector: 0 -1 -2

Press any key to continue . . .