美文网首页
6.binary_search查找

6.binary_search查找

作者: lxr_ | 来源:发表于2021-05-06 11:46 被阅读0次
#include<iostream>
#include<Windows.h>
using namespace std;

#include<vector>
#include<algorithm>

//二分查找法(必须在有序的序列中查找)
//bool binary_search(iterator begin, iterator end, value);
//查找指定的元素value,找到返回true,否则返回false

void test0601()
{
    vector<int> v;

    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    //v.push_back(2);如果是无序序列,查找结果未知
    
    double t0 = GetTickCount()/1000.0;//测量不出来,无法精确到us
    int ret=binary_search(v.begin(), v.end(), 9);
    double t1 = GetTickCount()/1000.0 - t0;
    if (ret)
    {
        cout << "找到了" <<"用了"<< t1 << endl;
    }
    else
    {
        cout << "未找到" << endl;
    }
    

    //对比二分法查找和普通查找的速度
    t0 = GetCurrentTime() / 1000.0;
    vector<int>::iterator it=find(v.begin(), v.end(), 5);
    t1 = GetTickCount()/1000.0 - t0;

    if (it == v.end())
    {
        cout << "未找到" << endl;
    }
    else
    {
        cout << "找到了" << "用了" << t1 << endl;
    }
}
int main()
{
    test0601();

    system("pause");
    return 0;
}

相关文章

  • 6.binary_search查找

  • 《数据结构与算法》知识点(四)

    第七章 查找 顺序查找、折半查找、索引查找、分块查找是静态查找,动态查找有二叉排序树查找,最优二叉树查找,键树查找...

  • 查找

    静态查找顺序查找 折半查找 散列查找 动态查找二叉排序树 散列查找 ASL(平均查找长度) - 衡量查找算法效率的...

  • PHP查找算法

    静态查找 顺序查找 折半查找 递归折半查找

  • 6.1 查找算法_基础

    1. 查找基本概念 查找:只有两种情况,查找成功,查找失败 查找表:查找的数据集合称为查找表 静态查找表 / 动态...

  • 据结构与算法学习-查找与二叉排序树

    查找表操作方式分为静态查找和动态查找。静态查找表(Static Search Table): 只作查找操作的查找表...

  • iOS-字符串查找

    字符串查找通常有四种方式,暴力查找,KMP查找,BoyerMoore查找以及RabinKarp算法查找,查找最简单...

  • linux 查找目录或文件

    查找目录:find /(查找范围) -name '查找关键字' -type d查找文件:find /(查找范围) ...

  • Linux查找文件、文件夹

    查找目录:find /(查找范围) -name '查找关键字' -type d查找文件:find /(查找范围) ...

  • linux常用命令

    查找目录:find /(查找范围) -name '查找关键字' -type d查找文件:find /(查找范围) ...

网友评论

      本文标题:6.binary_search查找

      本文链接:https://www.haomeiwen.com/subject/dliirltx.html