C++ STL algorithm, lower_bound() program example
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++ lower_bound() to search the position of the first element in an ordered range that has a value less than or equivalent to a specified value, where the ordering criterion may be specified by a binary predicate in C++ programming
To show: How to use the C++ algorithm, lower_bound() to search the position of the first element in an ordered range that has a value less than or equivalent to a specified value, where the ordering criterion may be specified by a binary predicate in C++ programming
// C++ STL algorithm, lower_bound()
#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)
{
// container
vector <int> vec1;
//iterators
vector <int>::iterator Iter1, Result1;
int i, j;
// constructing vectors vec1a & vec1b with default less than ordering
//
// pushing data
for(i = -3; i <= 6; i++)
vec1.push_back(i);
for(j =-5; j <= 2; j++)
vec1.push_back(j);
// some operations
cout<<"Operation: sort(vec1.begin(), vec1.end())"<<endl;
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 vectors vec2 with range sorted by greater
//
// container
vector <int> vec2(vec1);
// iterators
vector <int>::iterator Iter2, Result2;
cout<<"\nOperation: sort(vec2.begin(), vec2.end(), greater<int>())"<<endl;
sort(vec2.begin(), vec2.end(), greater<int>());
cout<<"vec2 vector data with range sorted by the binary predicate greater is: ";
for(Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)
cout<<*Iter2<<" ";
cout<<endl;
// constructing vectors vec3 with range sorted by mod_lesser()
//
// container
vector <int> vec3(vec1);
// iterators
vector <int>::iterator Iter3, Result3;
cout<<"\nOperation: sort(vec3.begin(), vec3.end(), mod_lesser)"<<endl;
sort(vec3.begin(), vec3.end(), mod_lesser);
cout<<"vec3 vector data with range sorted by the binary predicate mod_lesser is: ";
for(Iter3 = vec3.begin(); Iter3 != vec3.end(); Iter3++)
cout<<*Iter3<<" ";
cout<<endl;
// lower_bound of 5 in vec1 with default binary predicate less <int>()
cout<<"\nOperation: lower_bound(vec1.begin(), vec1.end(), 5)"<<endl;
Result1 = lower_bound(vec1.begin(), vec1.end(), 5);
cout<<"The lower_bound in vec2 vector for the element with a value of 5 is: "<<*Result1<<endl;
// lower_bound of 5 in vec2 with the binary predicate greater<int>()
Result2 = lower_bound(vec2.begin(), vec2.end(), 5, greater<int>());
cout<<"The lower_bound in vec2 vector for the element with a value of 5 is: "<<*Result2<<endl;
// lower_bound of 5 in vec3 with the binary predicate mod_lesser
cout<<"\nOperation: lower_bound(vec3.begin(), vec3.end(), 5, mod_lesser)"<<endl;
Result3 = lower_bound(vec3.begin(), vec3.end(), 5, mod_lesser);
cout<<"The lower_bound in vec3 vector for the element with a value of 5 is: "<<*Result3<<endl;
return 0;
}
Output examples:
Operation: sort(vec1.begin(), vec1.end())
vec1 vector data with range sorted by the binary predicate less than is: -5 -4 -3 -3 -2 -2 -1 -1 0 0 1 1 2 2 3 4 5 6
Operation: sort(vec2.begin(), vec2.end(), greater<int>())
vec2 vector data with range sorted by the binary predicate greater is: 6 5 4 3 2 2 1 1 0 0 -1 -1 -2 -2 -3 -3 -4 -5
Operation: sort(vec3.begin(), vec3.end(), mod_lesser)
vec3 vector data with range sorted by the binary predicate mod_lesser is: 0 0 -1 -1 1 1 -2 -2 2 2 -3 -3 3 -4 4 -5 5 6
Operation: lower_bound(vec1.begin(), vec1.end(), 5)
The lower_bound in vec2 vector for the element with a value of 5 is: 5
The lower_bound in vec2 vector for the element with a value of 5 is: 5
Operation: lower_bound(vec3.begin(), vec3.end(), 5, mod_lesser)
The lower_bound in vec3 vector for the element with a value of 5 is: -5
Press any key to continue . . .