• 泛型算法(十九)之搜索算法


    1、ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last):在序列中发现第一对相邻且值相等的元素。

        std::vector<int> c = {0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9};
        
        //在c中发现第一对相邻且值相等的元素
        auto iter = std::adjacent_find(c.begin(), c.end());
        //输出
        std::cout << *iter;
        //打印结果:4

    2、ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred):重载版本。用给定谓词pred代替了operator==

    自己实现pred,向算法定制操作。

    3、binary_search(ForwardIterator first, ForwardIterator last, const T& val):对一个升序序列做二分搜索,判定序列中是否有给定值val。

        std::vector<int> c = {0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9};
        
        //判断c中是否有值为5的元素
        bool i = std::binary_search(c.begin(), c.end(), 5);
        //输出
        std::cout << (int)i;
        //打印结果:1

    4、binary_search(ForwardIterator first, ForwardIterator last, const T& val, Compare comp):重载版本。用给定谓词pred代替了operator==

    自己实现comp,向算法定制操作。

    5、find(InputIterator first, InputIterator last, const T& val):对一个输入序列,找到第一个等于给定值的元素。

        std::vector<int> c = {0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9};
        
        //在c中找到第一个值为5的元素
        auto iter = std::find(c.begin(), c.end(), 5);
        //输出
        std::cout << *iter;
        //打印结果:5

    6、ForwardIterator1 find_end(ForwardIterator first1, ForwardIterator last1, ForwardIterator2 first2, ForwardIterator last2):在序列[first1, last1)中,找到序列[first2, last2)最后出现的位置。

        std::vector<int> c1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        std::vector<int> c2 = {5, 6, 7};
        
        //在c1中找到c2最后出现的位置
        auto iter = std::find_end(c1.begin(), c1.end(), c2.begin(), c2.end());
        //输出
        std::cout << *iter;
        //打印结果:5

    7、ForwardIterator1 find_end(ForwardIterator first1, ForwardIterator last1, ForwardIterator2 first2, ForwardIterator last2, BinaryPredicate pred):重载版本,用给定谓词pred代替operator==

    自己实现pred,向算法定制操作。

    8、find_first_of(InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2):在序列[first1, last1)中,找到集合[first2,last2)中任何一个元素的第一次出现。

        std::vector<int> c1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        std::vector<int> c2 = {5, 6, 7};
        
        //在c1中找到c2最后出现的位置
        auto iter = std::find_first_of(c1.begin(), c1.end(), c2.begin(), c2.end());
        //输出
        std::cout << *iter;
        //打印结果:5

    9、find_first_of(InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2, BinaryPredicate pred):重载版本,用给定谓词pred代替operator==

    自己实现pred,向算法定制操作。

    10、find_if(InputIterator first, InputIterator last, UnaryPredicate pred):在序列中返回满足谓词pred的第一个元素

        std::vector<int> c = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        
        //查找c中第一个大于5的元素
        auto iter = std::find_if(c.begin(), c.end(), [](int element){
            return element > 5;
        });
        //输出
        std::cout << *iter;
        //打印结果:6

    11、find_if_not(InputIterator first, InputIterator last, UnaryPredicate pred):C11算法,在序列中返回不满足谓词pred的第一个元素

        std::vector<int> c = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        
        //查找c中第一个不大于5的元素
        auto iter = std::find_if_not(c.begin(), c.end(), [](int element){
            return element > 5;
        });
        //输出
        std::cout << *iter;
        //打印结果:0

    12、search(ForwardIterator1 first1, ForwardIterator last1, ForwardIterator2 first2, ForwardIterator2 last2):在序列[first1,last1)中,找到序列[first2,last2)首次出现的位置

        std::vector<int> c1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        std::vector<int> c2 = {3, 4, 5};
        //在序列c1中找到首次出现序列c2的位置
        auto iter = std::search(c1.begin(), c1.end(), c2.begin(), c2.end());
        //输出
        std::cout << *iter;
        //打印结果:3

    13、search(ForwardIterator1 first1, ForwardIterator last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred):重载版本,用给定谓词pred代替operator==

    自己实现pred,向算法定制操作。

    14、search_n(ForwardIterator first, ForwardIterator last, Size count, const T& val):给定序列中,搜索给定值val连续出现n次的位置。

        std::vector<int> c = {0, 1, 2, 1, 1, 3, 1, 1, 1, 3, 1, 1, 1, 1, 2};
        //值为1的元素连续出现2次的位置
        auto iter = std::search_n(c.begin(), c.end(), 2, 1);
        std::cout << (iter - c.begin());
    
        std::cout << std::endl;
    
        //值为1的元素连续出现3次的位置
        iter = std::search_n(c.begin(), c.end(), 3, 1);
        std::cout << (iter - c.begin());
    
        std::cout << std::endl;
    
        //值为1的元素连续出现4次的位置
        iter = std::search_n(c.begin(), c.end(), 4, 1);
        std::cout << (iter - c.begin());
    
        /*打印结果:3
                  6
                  10
        */

    15、search_n(ForwardIterator first, ForwardIterator last, Size count, const T& val, BinaryPredicate pred),重载版本,用给定谓词pred代替operator==

     自己实现pred,向STL算法定制操作。

  • 相关阅读:
    在 WF 4 中编写自定义控制流活动
    CLS(公共语言规范)的CLSCompliant(跨语言调用)
    public类型中internal成员
    最短路径—Dijkstra算法和Floyd算法
    System.Windows.Forms
    第一个Xamarin的 Android 应用程序!
    内地开源镜像网站
    Xamarin C# Android for Windows 安装
    TortoiseSVN 源代码下载
    Install Visual Studio Tools for Apache Cordova
  • 原文地址:https://www.cnblogs.com/dongerlei/p/5145833.html
Copyright © 2020-2023  润新知