C++ STL algorithm, includes() code 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++ includes() to test whether one sorted range contains all the elements contained in a second sorted range, where the ordering or equivalence criterion between elements may be specified by a binary predicate in C++ programming

To show: How to use the C++ algorithm, includes() to test whether one sorted range contains all the elements contained in a second sorted range, where the ordering or equivalence criterion between elements may be specified by a binary predicate in C++ programming

// C++ STL algorithm, includes()

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

{

// containers

vector <int> vec1, vec2;

// iterators

vector <int>::iterator Iter1, Iter2;

bool Result1, Result2, Result3;

int i, j;

// constructing vec1 & vec2 vectors with default less-than ordering

//

// pushing data

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

vec1.push_back(i);

for(j =-2; j <= 3; j++)

vec2.push_back(j);

// printing the data

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

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

cout<<*Iter1<<" ";

cout<<endl;

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

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

cout<<*Iter2<<" ";

cout<<endl;

// constructing vec3 & vec4 vectors with ranges sorted by greater

//

// containers

vector <int> vec3(vec1), vec4(vec2);

// iterators

vector <int>::iterator Iter3, Iter4;

// do some sorting

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

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

// pop up a data

vec3.pop_back();

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

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

cout<<*Iter3<<" ";

cout<<endl;

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

for(Iter4 = vec4.begin(); Iter4 != vec4.end(); Iter4++)

cout<<*Iter4<<" ";

cout<<endl;

// constructing vec5 & vec6 vectors with ranges sorted by mod_lesser()

//

// containers

vector <int> vec5(vec1), vec6(vec2);

// iterators

vector <int>::iterator Iter5, Iter6;

// do some reversing

reverse(vec5.begin(), vec5.end());

// some popping up the data

vec5.pop_back();

vec5.pop_back();

// do some sorting

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

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

// print the data

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

for(Iter5 = vec5.begin(); Iter5 != vec5.end(); Iter5++)

cout<<*Iter5<<" ";

cout<<endl;

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

for(Iter6 = vec6.begin(); Iter6 != vec6.end(); Iter6++)

cout<<*Iter6<<" ";

cout<<endl;

// to test for inclusion under an ascending order with the default binary predicate less <int>()

Result1 = includes(vec1.begin(), vec1.end(), vec2.begin(), vec2.end());

if(Result1)

cout<<"\nAll the elements in vec2 vector are contained in vec1 vector"<<endl;

else

cout<<"\nAt least one of the elements in vec2 vector is not contained in vec1 vector"<<endl;

// to test for inclusion under descending order specifies binary predicate greater<int>()

Result2 = includes(vec3.begin(), vec3.end(), vec4.begin(), vec4.end(), greater<int>());

if(Result2)

cout<<"\nAll the elements in vec4 vector are contained in vec3 vector"<<endl;

else

cout<<"\nAt least one of the elements in vec4 vector is not contained in vec3 vector"<<endl;

// to test for inclusion under a user defined binary predicate mod_lesser()

Result3 = includes(vec5.begin(), vec5.end(), vec6.begin(), vec6.end(), mod_lesser);

if(Result3)

cout<<"\nAll the elements in vec6 vector are contained under mod_lesser() in vec5 vector"<<endl;

else

cout<<"\nAt least one of the elements in vec6 vector is not contained under mod_lesser() in vec5 vector"<<endl;

return 0;

}

Output examples:

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

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

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

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

vec5 vector data with range sorted by the binary predicate mod_lesser() is: 0 1 2 3 4

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

All the elements in vec2 vector are contained in vec1 vector

At least one of the elements in vec4 vector is not contained in vec3 vector

At least one of the elements in vec6 vector is not contained under mod_lesser() in vec5 vector

Press any key to continue . . .