C++ //常用算法 binary_serach //查找指定的元素 //无序序列中不可用

 1 //常用算法 binary_serach
 2 //查找指定的元素
 3 //无序序列中不可用
 4 
 5 
 6 #include<iostream>
 7 #include<algorithm>
 8 #include<vector>
 9 
10 using namespace std;
11 
12 void test01()
13 {
14     vector<int>v;
15     for (int i = 0; i < 10; i++)
16     {
17         v.push_back(i);
18     }
19 
20     //查找容器中是否有9
21      bool ret =    binary_search(v.begin(), v.end(), 9);
22      if (ret)
23      {
24          cout << "找到了!" << endl;
25      }
26      else
27      {
28          cout << "未找到!" << endl;
29      }
30 }
31 
32 
33 
34 int main()
35 {
36 
37     test01();
38     system("pause");
39     return 0;
40 }

 

posted on 2021-08-18 10:59  Bytezero!  阅读(60)  评论(0编辑  收藏  举报