STL algorithmアルゴリズムbinary_search(5)


原文の住所:http://www.cplusplus.com/reference/algorithm/binary_search/
function template
std::binary_search
default(1)
template 
  bool binary_search (ForwardIterator first, ForwardIterator last,
                      const T& val);
custom(2)
template 
  bool binary_search (ForwardIterator first, ForwardIterator last,
                      const T& val, Compare comp);
Test if value exists in sorted sequence
Returns  true if any element in the range  [first,last) is equivalent to val,and  false others wise.
もし[first,end]の範囲内に任意の要素とvalが等しいとtrueに戻ります.そうでなければfalseに戻ります.
例:
#include 
#include 
#include 
using namespace std;
void binary_s(){

    vector vi;
    for(int i=0;i<9;i++)
        vi.push_back(i);


    if(binary_search(vi.begin(),vi.end(),1))
        cout<
はスクリーンショットを実行します.
STL algorithm算法binary_search(5)_第1张图片
The elements are compred using  operator< for the first version,and comp for the second.Two elemens、 a. and b ar consided equivalent if  (!(a or if (!comp(a,b) && !comp(b,a)).
のバージョンの の の (!(a)
The elements in the range share already be sorted accoding to this same criterion(operator<) or comp)、or at least partitioned with respect to val.
の は べ えられているはずです. じ で べ えられています. comp)は、 なくともvalに べられます.
:
#include 
#include 
#include 
using namespace std;
void binary_s2(){

    vector 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<
はスクリーンショットを します.
STL algorithm  binary_search(5)_ 2   は られます.7は つけられますが、2は つけられません.
The function optimizes the number of comprisos performed by compring non-consecutive element of the sorted range,which is specially efficient for ラドドm-access iterators.
この は な を する を し,ランダムアクセスはより である.
The behavior of this function template is equivalent to:
この の は のようです.
1
2
3
4
5
6
template 
  bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)
{
  first = std::lower_bound(first,last,val);
  return (first!=last && !(val
パラメータ
ファースト
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 elemens between ファースト and last,including the element pointed by ファースト but not the element pointed by last.シーケンスの と を べ えるには、firstを む「first」のすべての が まれていますが、lastが す は まれていません.
val
Value to search for in the range.For (1) T sharll be a type supporting being compred with elemens of the range  [first,last) as either operand of  operator<. する .(1)については、 の は けされているはずである.
comp
Binary function that accepts two argments of the type pointed by ForwardIterator (and of type T)、and returns a value convertible to  bool.The value returned indicates whethe r the first argment is consided to go before the second.The function sharll not modify any of argments.This can einther be a function pointer a function oject.2 の が されるべきかどうか.この はパラメータを するべきではありません.
Return valuetrue if an element equivalent to val is found,and  false others wise.
がvalと しい 、trueに ります.そうでなければfalseに ります.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// binary_search example
#include      // std::cout
#include     // std::binary_search, std::sort
#include        // std::vector

bool myfunction (int i,int j) { return (i v(myints,myints+9);                         // 1 2 3 4 5 4 3 2 1

  // using default comparison:
  std::sort (v.begin(), v.end());

  std::cout << "looking for a 3... ";
  if (std::binary_search (v.begin(), v.end(), 3))
    std::cout << "found!
"; else std::cout << "not found.
"; // using myfunction as comp: std::sort (v.begin(), v.end(), myfunction); std::cout << "looking for a 6... "; if (std::binary_search (v.begin(), v.end(), 6, myfunction)) std::cout << "found!
"; else std::cout << "not found.
"; return 0; }
Edit&Run
Output:
looking for a 3... found!
looking for a 6... not found.
Coplexity
On average,logirithmic in the distance between ファースト and last:Performs approximatiely  log2(N)+2 element compparisos(where) N is this distance)
On non-random-access iterators、the iterator advances produce themselves an additional linea coplexity in N on average.
Data races
The object in the range  [first,last) araccessed.
Exceptions
Throws if eiher an element comprisor or an operation on on an iterator throws.
Note that invalid argments cause undefined behavior.
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// いた いや いところを えてください. のメッセージを したり、 のメールアドレスをクリックしてメールしてください. の いと を してください. して、 さんにもっといいものを えてください.ありがとうございます.
は を してください.http://blog.csdn.net/qq844352155
author:
メール:[email protected]
2014-9-8
GDUTで
さん、 おめでとうございます.
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————