STL contains four different binary search algorithms, binary_search lower_bound upper_bound equal_range. Their range is sorted.
binary_search attempts to find the element value in the sorted [first, last]. If [first, last] has an element equivalent to value, it returns true, otherwise it returns false, and it does not return the location of the lookup.
Lower_bound It attempts to find the element value in the sorted [first, last]. If [first, last] has an element equivalent to value, lower_bound returns an iterator pointing to the first element. If no such element exists, it returns to the position where it would appear if such an element were assumed to exist. That is, point to the first element not less than value. If value is greater than any element of [first, last], then last is returned.
Up_bound tries to find value in the sorted [first, last] and returns the last appropriate location for the placeable value. If value exists, lower_bound returns an iterator to that element. By contrast, upper_bound does not do this, returning to the last appropriate place where value can be inserted. If value exists, the iterator it returns will point to the next location of value, not value itself.
The return value of equal_range essentially combines the return values of lower_bound and upper_bound. Its return value is a pair of iterators I and j, where I is the first place where value can be inserted and j is the last place where value can be inserted. It can be deduced that every element in [i, j] is equivalent to value, and [i, j] is a maximum subinterval of [first, last] which conforms to the above properties. The algorithm lower_bound returns the first iterator of the range, the algorithm upper_bound returns the past-the-end iterator of the range, and the algorithm equal_range returns both in the form of pairs.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
vector<int>::iterator iter;
pair<vector<int>::iterator, vector<int>::iterator> vecpair;
for(int i = 1; i<= 20; i++) {
v.push_back(i%6);
}
sort(v.begin(), v.end());
cout << "array: " << endl << " ";
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl << endl;
/* lower_bound */
cout << "lower_bound function, value = 3: " << endl;
iter = lower_bound(v.begin(), v.end(), 3);
cout << " [first, iter] = ";
copy(v.begin(), iter, ostream_iterator<int>(cout, " "));
cout << endl;
cout << " [iter, last] = ";
copy(iter, v.end(), ostream_iterator<int>(cout, " "));
cout << endl << endl;
/* upper_bound */
cout << "upper_bound function, value = 3: " << endl;
iter = upper_bound(v.begin(), v.end(), 3);
cout << " [first, iter] = ";
copy(v.begin(), iter, ostream_iterator<int>(cout, " "));
cout << endl;
cout << " [iter, last] = ";
copy(iter, v.end(), ostream_iterator<int>(cout, " "));
cout << endl << endl;
/* equal_range */
cout << "euqual_range function value = 3: " << endl;
vecpair = equal_range(v.begin(), v.end(), 3);
cout << " [vecpair->first, vecpair->second] = ";
copy(vecpair.first, vecpair.second, ostream_iterator<int>(cout, " "));
cout << endl << endl;
/* binary_search */
cout << "binary_search function value = 3: " << endl;
cout << "3 is " << (binary_search(v.begin(), v.end(), 3) ? "": "not ") << " in array" << endl;
cout << endl;
/* binary_search */
cout << "binary_search function value = 6: " << endl;
cout << "6 is " << (binary_search(v.begin(), v.end(), 6) ? "": "not ") << " in array" << endl;
cout << endl;
}
/*./bsearch
array:
0 0 0 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 5
lower_bound function, value = 3:
[first, iter] = 0 0 0 1 1 1 1 2 2 2 2
[iter, last] = 3 3 3 4 4 4 5 5 5
upper_bound function, value = 3:
[first, iter] = 0 0 0 1 1 1 1 2 2 2 2 3 3 3
[iter, last] = 4 4 4 5 5 5
euqual_range function value = 3:
[vecpair->first, vecpair->second] = 3 3 3
binary_search function value = 3:
3 is in array
binary_search function value = 6:
6 is not in array*/