default (1) | template <class ForwardIterator, class T>
bool binary_search (ForwardIterator first, ForwardIterator last,
const T& val);
|
---|---|
custom (2) | template <class ForwardIterator, class T, class Compare>
bool binary_search (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp); |
true
if any element in the range [first,last)
is equivalent to val, and false
otherwise.如果在[first,end)范围内存在任一元素和val相等,则返回true,否则返回false.
例子:
#include <iostream> #include <vector> #include <algorithm> using namespace std; void binary_s(){ vector<int> vi; for(int i=0;i<9;i++) vi.push_back(i); if(binary_search(vi.begin(),vi.end(),1)) cout<<"1 exits!"<<endl; else cout<<"1 didn't exits!"<<endl; if(binary_search(vi.begin(),vi.end(),7)) cout<<"7 exits!"<<endl; else cout<<"7 didn't exits!"<<endl; if(binary_search(vi.begin(),vi.end(),10)) cout<<"10 exits!"<<endl; else cout<<"10 didn't exits!"<<endl; }运行截图:
operator<
for the first version, and comp for the second. Two elements, a and b are considered equivalent if (!(a<b) && !(b<a))
or if (!comp(a,b) && !comp(b,a))
.第一个版本里面元素的比较使用<比较符。第二个个使用comp比较器,两个元素,a和b相等的条件为(!(a<b) && !(b<a)).
The elements in the range shall already be sorted according to this same criterion (operator<
or comp), or at least partitioned with respect to val.
范围内的容器应该是已经排序好了的,并且是使用相同的规则排序的(operator< or comp),至少排序到val。(即最后一个排序好的元素是val).
例子:
#include <iostream> #include <vector> #include <algorithm> using namespace std; void binary_s2(){ vector<int> vi; vi.push_back(1); vi.push_back(3); vi.push_back(4); vi.push_back(7); vi.push_back(2); vi.push_back(5); /*vi (1,3,4,7,2,5)*/ if(binary_search(vi.begin(),vi.end(),1)) cout<<"1 exits!"<<endl; else cout<<"1 didn't exits!"<<endl; if(binary_search(vi.begin(),vi.end(),7)) cout<<"7 exits!"<<endl; else cout<<"7 didn't exits!"<<endl; if(binary_search(vi.begin(),vi.end(),2)) cout<<"2 exits!"<<endl; else cout<<"2 didn't exits!"<<endl; }运行截图:
该方法优化了比较不连续的元素的次数,随机访问迭代器则更为高效。
The behavior of this function template is equivalent to:
该函数的行为类似下面:
|
|
Forward iterators to the initial and final positions of a sorted (or properly partitioned) sequence. The range used is [first,last)
, which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
要排序序列的开头及结束位置,包括[first,last)里的所有元素,包括first,但不包括last指向的元素。
[first,last)
as either operand of operator<
.bool
. The value returned indicates whether the first argument is considered to go before the second.true
if an element equivalent to val is found, and false
otherwise. |
|
Edit & Run
|
looking for a 3... found! looking for a 6... not found. |
log2(N)+2
element comparisons (where N is this distance).On non-random-access iterators, the iterator advances produce themselves an additional linear complexity in N on average.
[first,last)
are accessed.Note that invalid arguments cause undefined behavior.
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:[email protected]
2014-9-8
于GDUT
祝大家中秋节快乐!
——————————————————————————————————————————————————————————————————