C++ STL algorithm, binary_search(), mod_lesser(), pushback() and, sort() 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++ binary_search() to test whether there is an element in a sorted range that is equal to a specified value or that is equivalent to it in a sense specified by a binary predicate in C++ programming
To show: How to use the C++ algorithm, binary_search(), mod_lesser(), pushback() and sort() to test whether there is an element in a sorted range that is equal to a specified value or that is equivalent to it in a sense specified by a binary predicate in C++ programming
// C++ STL algorithm, binary_search(), mod_lesser(), pushback() and sort()
#include <list>
#include <vector>
#include <algorithm>
#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)
{
// list container
list<int> lst;
// list iterator
list<int>::iterator Iter;
bool b1, b2;
int i;
// pushing data, constructing a list
lst.push_back(71);
lst.push_back(22);
lst.push_back(10);
lst.push_back(21);
lst.push_back(99);
lst.push_back(23);
lst.push_back(12);
// do some sorting
lst.sort();
// print the list
cout<<"lst list data: ";
for(Iter = lst.begin(); Iter != lst.end(); Iter++)
cout<<*Iter<<" ";
cout<<endl;
// default binary_search()
b1 = binary_search(lst.begin(), lst.end(), 22);
if(b1)
cout<<"\nThere is an element in lst list with a value equal to 22."<<endl;
else
cout<<"\nThere is no element in lst list with a value equal to 22."<<endl;
// a binary_search() under the binary predicate greater
lst.sort(greater<int>());
b2 = binary_search(lst.begin(), lst.end(), 23, greater<int>());
if(b2)
cout<<"\nThere is an element in lst list with a value equivalent to 23 under greater than."<<endl;
else
cout<<"\nNo element in lst list with a value equivalent to 23 under greater than."<<endl;
// a binary_search() under the user-defined binary predicate mod_lesser()
//
// vector container
vector <int> vec;
// vector iterator
vector <int>::iterator Iter1;
// push the data
for(i = -3; i <= 5; i++)
vec.push_back(i);
// do some sorting
sort(vec.begin(), vec.end(), mod_lesser);
// print the data
cout<<"\nOrdered under mod_lesser(), vec vector data: ";
for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
bool b3 = binary_search(vec.begin(), vec.end(), -2, mod_lesser);
if(b3)
cout<<"\nThere is an element with a value equivalent to -2 under mod_lesser()."<<endl;
else
cout<<"\nThere is no element with a value equivalent to -2 under mod_lesser()."<<endl;
return 0;
}
Output examples:
lst list data: 10 12 21 22 23 71 99
There is an element in lst list with a value equal to 22.
There is an element in lst list with a value equivalent to 23 under greater than.
Ordered under mod_lesser(), vec vector data: 0 -1 1 -2 2 -3 3 4 5
There is an element with a value equivalent to -2 under mod_lesser().
Press any key to continue . . .