C++ STL algorithm, equal_range() 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++ equal_range() to find a pair of positions in an ordered range, the first less than or equivalent to the position of a specified element and the second greater than the element's position, where the sense of equivalence or ordering used to establish the positions in the sequence may be specified by a binary predicate

To show: How to use the C++ algorithm, equal_range() to find a pair of positions in an ordered range, the first less than or equivalent to the position of a specified element and the second greater than the element's position, where the sense of equivalence or ordering used to establish the positions in the sequence may be specified by a binary predicate in C++ programming

// C++ STL algorithm, equal_range()

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

vector <int>::iterator Iter1;

pair < vector <int>::iterator, vector <int>::iterator > Result1, Result2, Result3;

int i, j;

// pushing data, constructing vectors vec1 with default less than ordering

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

vec1.push_back(i);

for(j =1; j <= 5; j++)

vec1.push_back(j);

// do some sorting

sort(vec1.begin(), vec1.end());

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;

// constructing vec2 vector with range sorted by greater

//

// vector container

vector <int> vec2(vec1);

// vector iterators

vector <int>::iterator Iter2;

// do some sorting

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

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

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

cout<<*Iter2<<" ";

cout<<endl;

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

//

// vector container

vector <int> vec3(vec1);

// vector iterator

vector <int>::iterator Iter3;

// do some sorting

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

// print the result

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

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

cout<<*Iter3<<" ";

cout<<endl<<endl;

// the equal_range() operation of 4 in vec1 vector with default binary predicate less <int>()

Result1 = equal_range(vec1.begin(), vec1.end(), 4);

// print the result

cout<<"lower_bound in vec1 for the element with a value of 4 is: "<<*Result1.first<<endl;

cout<<"upper_bound in vec1 for the element with a value of 4 is: "<<*Result1.second<<endl;

// another result

cout<<"The equivalent class for the element with a value of 4 in vec1 vector includes the elements: ";

for(Iter1 = Result1.first; Iter1 != Result1.second; Iter1++)

cout<<*Iter1<<" ";

cout<<endl<<endl;

// do the equal_range() operation of 4 in vec2 vector with the binary predicate greater <int>()

Result2 = equal_range(vec2.begin(), vec2.end(), 4, greater <int>());

cout<<"lower_bound in vec2 for the element with a value of 4 is: "<<*Result2.first<<endl;

cout<<"upper_bound in vec2 for the element with a value of 4 is: "<<*Result2.second<<endl;

// print result

cout<<"The equivalent class for the element with a value of 4 in vec2 vector includes the elements: ";

for(Iter2 = Result2.first; Iter2 != Result2.second; Iter2++)

cout<<*Iter2<<" ";

cout<<endl<<endl;

// do the equal_range() of 4 in vec3 vector with the binary predicate mod_lesser()

Result3 = equal_range(vec3.begin(), vec3.end(), 4, mod_lesser);

cout<<"lower_bound in vec3 for the element with a value of 4 is: "<<*Result3.first<<endl;

cout<<"upper_bound in vec3 for the element with a value of 4 is: "<<*Result3.second<<endl;

// print the result

cout<<"equivalent class for the element with a value of 4 in vec3 vector includes the elements: ";

for(Iter3 = Result3.first; Iter3 != Result3.second; Iter3++)

cout<<*Iter3<<" ";

cout<<endl<<endl;

return 0;

}

Output examples:

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

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

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

lower_bound in vec1 for the element with a value of 4 is: 4

upper_bound in vec1 for the element with a value of 4 is: 5

The equivalent class for the element with a value of 4 in vec1 vector includes the elements: 4 4

lower_bound in vec2 for the element with a value of 4 is: 4

upper_bound in vec2 for the element with a value of 4 is: 3

The equivalent class for the element with a value of 4 in vec2 vector includes the elements: 4 4

lower_bound in vec3 for the element with a value of 4 is: 4

upper_bound in vec3 for the element with a value of 4 is: 5

equivalent class for the element with a value of 4 in vec3 vector includes the elements: 4 4

Press any key to continue . . .