定义:
template <class ForwardIterator, class T> bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value );
template <class ForwardIterator, class T, class Compare> bool binary_search ( ForwardIterator first, ForwardIterator last, const T& value, Compare comp );
作用:
在[first, last)范围内,查找值为T的元素.
注意:
待查找的区间范围必须首先按大小进行排序
返回:
如果找到,则返回true;否则返回false
示例:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int myints[] = {1,2,3,4,5};
vector<int> v(myints,myints + 5);
cout << "looking for a 3... ";
if (binary_search (v.begin(), v.end(), 3))
cout << "found!\n";
else
cout << "not found.\n";
return 0;