• C++ STL 常用遍历算法


    C++ STL 常用遍历算法

    STL的容器算法迭代器的设计理念


    1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 
    2) STL的迭代器技术实现了遍历容器的统一方法;也为STL的算法提供了统一性奠定了基 础 
    3) STL的算法,通过函数对象实现了自定义数据类型的算法运算;所以说:STL的算法也提 供了统一性。 
                   核心思想:其实函数对象本质就是回调函数,回调函数的思想:就是任务的编写者和任务的调用者有效解耦合。函数指针做函数参数。
    4) 具体例子:transform算法的输入,通过迭代器first和last指向的元算作为输入;通过 result作为输出;通过函数对象来做自定义数据类型的运算。

    常用的遍历算法

    for_each() 

    for_each: 用指定函数依次对指定范围内所有元素进行迭代访问。该函数不得修改 序列中的元素。

    函数定义。 For_each(begin, end, func);

    template<class _InIt, class _Fn1>
    inline _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
    { // perform function for each element
            _DEBUG_RANGE(_First, _Last);
            _DEBUG_POINTER(_Func);
            return (_For_each(_Unchecked(_First), _Unchecked(_Last), _Func));
    }
    注意for_each的第三个参数 函数对象做函数参数,函数对象做返回值

    #define _CRT_SECURE_NO_WARNINGS
    
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    class CMyShow 
    { 
    public: 
        CMyShow()
        {  
            num = 0; 
        } 
        void operator()(const int &iItem) 
        {  
            num ++;   
            cout << iItem;  
        } 
        void printCount() 
        {  
            cout << "num:" << num << endl; 
        }  
    private: 
        int num; 
    };   
    
    void show(const int &iItem)
    {  
        cout << iItem;
    }
    
    
    void mytest()
    {
        int iArray[] = {0,1,2,3,4};
        vector<int> vecInt(iArray, iArray+sizeof(iArray)/sizeof(iArray[0]));
        for_each(vecInt.begin(), vecInt.end(), show); // 结果打印出0 1 2 3 4
    
        CMyShow show1 = for_each(vecInt.begin(), vecInt.end(), CMyShow());
        cout << endl;
        show1.printCount(); //显示对象被调用的次数
    
        return;
    }
    
    int main()
    {
        mytest();
    
        system("pause");
        return 0;
    }

    transform()

    transform: 与for_each类似,遍历所有元素,但可对容器的元素进行修改 

    transform()算法有两种形式:
           transform(b1, e1, b2, op) 
           transform(b1, e1, b2, b3, op)

    template<class _InIt, class _OutIt, class _Fn1>
    inline _OutIt transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func)

    transform()的作用
    例如:可以一个容器的元素,通过op,变换到另一个容器中(同一个容器中) 也可以把两个容器的元素,通过op,变换到另一个容器中

    注意:
    1.如果目标与源相同,transform()就和for_each()一样。
    2.如果想以某值替换符合规则的元素,应使用replace()算法

    #define _CRT_SECURE_NO_WARNINGS
    
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <functional>
    #include <vector>
    
    using namespace std;
    
    int increase(int i)
    {
        return i+1;
    }
    
    void mytest()
    {
        vector<int> vecIntA;
        vecIntA.push_back(1);
        vecIntA.push_back(3);
        vecIntA.push_back(5);
        vecIntA.push_back(7);
        vecIntA.push_back(9);
    
        transform(vecIntA.begin(), vecIntA.end(), vecIntA.begin(), increase);
        for_each(vecIntA.begin(), vecIntA.end(), [](const int & val){ cout << val << " ";}); // 2,4,6,8,10
    
        transform(vecIntA.begin(), vecIntA.end(), vecIntA.begin(), negate<int>());
        for_each(vecIntA.begin(), vecIntA.end(), [](const int & val){ cout << val << " ";}); // -2, -4, -6, -8, -10
    
        return;
    }
    
    int main()
    {
        mytest();
    
        system("pause");
        return 0;
    }

    for_each()和transform()算法比较

    1)STL 算法 – 修改性算法
    for_each()
    copy()
    copy_backward()
    transform()
    merge()
    swap_ranges()
    fill()
    fill_n()
    generate()
    generate_n()
    replace
    replace_if()
    replace_copy()
    replace_copy_if()

    2)
    for_each() 速度快 不灵活
    transform() 速度慢 非常灵活

    //一般情况下:for_each所使用的函数对象,参数是引用,没有返回值
    void mysquare(int &num)
    {
            num = num * num;
    }

    //transform所使用的函数对象,参数一般不使用引用,而是还有返回值
    int mysquare2(int num) //结果的传出,必须是通过返回值
    {
           return num = num * num;
    }

    void main()
    {
           vector<int> v1;
           v1.push_back(1);
           v1.push_back(3);
           v1.push_back(5);
           vector<int>v2 = v1;

           for_each(v1.begin(), v1.end(), mysquare); printAA(v1);
           cout << endl;

           transform(v2.begin(), v2.end(), v2.begin(), mysquare2); printAA(v2);
           cout << endl;
    }

  • 相关阅读:
    堆的创建、优先队列、topk、堆排序C语言实现
    HTTPS加密原理
    go shard map实现
    Python进程间通信
    TCP 半连接队列和全连接队列
    WireShark过滤语法
    TCP拥塞机制
    【企业管理实务系列】低值易耗品管理办法
    CV之Face Change:基于人工智能实现国内众多一线美女明星换脸(基于Face++输出4*106个特征点定位+融合代码、deepfake技术)
    【转发】农行银企直联XML对接socket SAP EPIC
  • 原文地址:https://www.cnblogs.com/lsgxeva/p/7792670.html
Copyright © 2020-2023  润新知