美文网首页
STL算法之常用查找

STL算法之常用查找

作者: 二进制人类 | 来源:发表于2022-10-17 15:17 被阅读0次

    find

    API

    /*
    find 算法 查找元素
    @param beg 容器开始迭代器
    @param end 容器结束迭代器
    @param value 查找的元素
    @return 迭代器
    */
    find(iterator beg, iterator end, value);
    

    实例

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    using  namespace std;
    int main() {
        vector<int> v;
        v.push_back(10);
        v.push_back(20);
        v.push_back(30);
        v.push_back(40);
        v.push_back(50);
        vector<int>::iterator ret = find(v.begin(),v.end(),30);
        if(ret!=v.end()){
            cout<<*ret<<endl;
        }
        return 0;
    }
    

    find_if

    API

    /*
    find_if 算法 条件查找
    @param beg 容器开始迭代器
    @param end 容器结束迭代器
    @param callback 回调函数或者谓词(返回 bool 类型的函数对象)
    @return  迭代器
    */
    find_if(iterator beg, iterator end, _callback)
    

    实例

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    using namespace std;
    bool callback(int num){
         return num > 25;
     }
    int main(){
        vector<int> v;
        v.push_back(10);
        v.push_back(20);
        v.push_back(30);
        v.push_back(40);
        v.push_back(50);
        vector<int>::iterator it = find_if(v.begin(),v.end(),callback);
        if (it != v.end())
        {
            cout<< *it << endl;//30
        }else{
        }
        return 0;
    }
    

    adjacent_find

    API

    /*
    adjacent_find 算法 查找相邻重复元素
    @param beg 容器开始迭代器
    @param end 容器结束迭代器
    @param _callback 回调函数或者谓词(返回 bool 类型的函数对象)
    @return 返回相邻元素的第一个位置的迭代器
    */
    adjacent_find(iterator beg, iterator end, _callback);
    

    实例

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    using namespace std;
    bool callback(int num){
         return num > 25;
     }
    int main(){
        vector<int> v;
        v.push_back(10);
        v.push_back(20);
        v.push_back(20);
        v.push_back(30);
        v.push_back(30);
        vector<int>::iterator it = adjacent_find(v.begin(),v.end());
        if (it != v.end())
        {
            cout<< *it << endl; //20
        }else{
    
        }
        return 0;
    }
    
    

    binary_search

    API

    /*
    binary_search 算法 二分查找法
    注意: 在无序序列中不可用
    @param beg 容器开始迭代器
    @param end 容器结束迭代器
    @param value 查找的元素
    @return bool 查找返回 true 否则 false
    */
    bool binary_search(iterator beg, iterator end, value);
    

    实例

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    using namespace std;
    bool callback(int num){
         return num > 25;
     }
    int main(){
        vector<int> v;
        v.push_back(10);
        v.push_back(20);
        v.push_back(30);
        v.push_back(40);
        v.push_back(50);
        bool ret = binary_search(v.begin(),v.end(),20);
        if (ret)
        {
            cout<<"找到"<<endl; 
        }else{
            cout<<"未找到"<<endl;
        }
        return 0;
    }
    

    count

    API

    /*
    count 算法 统计元素出现次数
    @param beg 容器开始迭代器
    @param end 容器结束迭代器
    @param value 回调函数或者谓词(返回 bool 类型的函数对象)
    @return int 返回元素个数
    */
    count(iterator beg, iterator end, value);
    

    实例

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    using namespace std;
    // bool callback(int num){
    //      return num > 25;
    //  }
    int main(){
        vector<int> v;
        v.push_back(10);
        v.push_back(20);
        v.push_back(30);
        v.push_back(20);
        v.push_back(20);
        int ret = count(v.begin(),v.end(),20);
        cout<<ret<<endl;//3
        return 0;
    }
    

    count_if

    API

    /*
    count_if 算法 统计元素出现次数
    @param beg 容器开始迭代器
    @param end 容器结束迭代器
    @param callback 回调函数或者谓词(返回 bool 类型的函数对象)
    @return int 返回元素个数
    */
    count_if(iterator beg, iterator end, _callback);
    

    实例

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    using namespace std;
    bool callback(int num){
         return num > 15;
     }
    int main(){
        vector<int> v;
        v.push_back(10);
        v.push_back(20);
        v.push_back(30);
        v.push_back(20);
        v.push_back(20);
        int ret = count_if(v.begin(),v.end(),callback);
        cout<<ret<<endl;//4
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:STL算法之常用查找

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