• 【c++算法】变动性算法


    直接改变元素值,或者在复制到另一区间的过程中改变元素值

     

    For_each

    针对每个元素执行某项操作

    Copy

    从第一个元素开始复制某段区间

    Copy_backward

    从最后一个元素开始复制某段区间

    Transform

    变动(并复制)元素,将两个区间元素合并

    Merge

    合并两个区间

    Swap_ranges

    交换区间内的元素

    Fill

    以给定值替换每个元素

    Finn_n

    以给定值替换n个元素

    Generate

    以某项操作的结果替换每个元素

    Generate_n

    以某项操作的结果替换n个元素

    Replace

    将具有某特定值的元素替换为另一个值

    Replace_if

    将符合条件的元素替换为另一个值

    Replace_copy

    复制整个区间,同时将具有某特定值的元素替换为另一个值

    Replace_copy_if

    复制整个区间,同时将符合条件的元素替换为另一个值


    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    #include <list>

    using namespace std;

    template<class T>
    class FunctionObjectType
    {
    private:
        T theValue;
    public:
        FunctionObjectType(const T& v):theValue(v)
        {

        }
    public:
        void operator()(T &i)
        {
            i += theValue;
        }
    };
    /*--------------打印元素-------------*/
    template<class T>
    inline void PrintElements(const T& coll,const char* optcstr="")
    {
        typename T::const_iterator pos;

        std::cout<<optcstr;
        for(pos=coll.begin();pos != coll.end(); ++pos)
            std::cout<<*pos<<" ";

        std::cout<<std::endl;
    }

    template<class T>
    inline void PrintElementsPos(const T& coll,const char* optcstr="")
    {
        typename T::const_iterator pos;

        std::cout<<optcstr;
        for(pos=coll.begin();pos != coll.end(); ++pos)
            std::cout<<&*pos<<" ";

        std::cout<<std::endl;
    }

    /*--------------插入int型数据------------------*/
    template<class T>
    inline void InsertElements(T& coll,int first,int last)
    {
        for(int i = first; i<=last; ++i)
            coll.insert(coll.end(),i);
    }

    void print(int elem)
    {
        std::cout<< elem << " ";
    }

    //for_each对每个元素执行操作
    void Learn_for_each2()
    {
        std::cout<<std::endl<<"--------------------in Learn for_each2()"<<endl;

        vector<int> coll;

        InsertElements(coll,1,9);
        PrintElements(coll,"coll : ");

        std::cout<<"每个元素与第一个元素求和"<<endl;
        for_each(coll.begin(),coll.end(),
                FunctionObjectType<int>(*coll.begin()));

        PrintElements(coll,"coll : ");

        std::cout<<endl;
    }

    void Learn_copy()
    {
        std::cout<<std::endl<<"--------------------in Learn copy() copy_backward()"<<endl;

        vector<int> coll1;
        list<int> coll2;

        InsertElements(coll1,1,9);
        PrintElements(coll1,"coll1 : ");

        std::cout<<"使用back_inserter把coll1拷贝到coll2"<<std::endl;
        copy(coll1.begin(),coll1.end(),back_inserter(coll2));
        PrintElements(coll2,"coll2 : ");
        coll2.clear();

        std::cout<<"使用front_inserter把coll1拷贝到coll2"<<std::endl;
        copy(coll1.begin(),coll1.end(),front_inserter(coll2));
        PrintElements(coll2,"coll2 : ");
        coll2.clear();

        std::cout<<"使用inserter把coll1拷贝到coll2,从第开始处插入"<<std::endl;
        copy(coll1.begin(),coll1.end(),inserter(coll2,coll2.begin()));
        PrintElements(coll2,"coll2 : ");
        //coll2.clear();

        std::cout<<"把coll1拷贝到coll2,从第二个元素开始覆盖"<<std::endl;
        copy(coll1.begin(),coll1.end(),++coll2.begin());
        PrintElements(coll2,"coll2 : ");
        //coll2.clear();

        std::cout<<"使用ostream_iterator,cout"<<std::endl;
        copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
        std::cout<<std::endl;
        coll2.clear();

        vector<char> source(10,'.');
        for(int c='a';c<='f';c++)
        {
            source.push_back(c);
        }
        source.insert(source.end(),10,'.');
        PrintElements(source,"source  :  ");

        std::cout<<"从位置7开始copy"<<std::endl;
        vector<char> c1(source.begin(),source.end());
        copy(c1.begin()+10,c1.begin()+16,c1.begin()+7);
        PrintElements(c1,"coll3  :  ");

        std::cout<<"从位置7开始backward copy"<<std::endl;
        vector<char> c2(source.begin(),source.end());
        copy_backward(c2.begin()+10,c2.begin()+16,c2.begin()+7);
        PrintElements(c2,"coll3  :  ");
    }

    void Learn_transforming()
    {
        std::cout<<std::endl<<"--------------------in Learn transforming()"<<endl;
        std::cout<<"复制修改元素一气完成"<<endl;

        vector<int> coll1;
        list<int> coll2;

        InsertElements(coll1,1,9);
        PrintElements(coll1,"coll1 : ");

        std::cout<<"求反 : "<<std::endl;
        transform(coll1.begin(),coll1.end(),coll1.begin(),negate<int>());
        PrintElements(coll1,"coll1 negate : ");

        std::cout<<"10倍 : "<<std::endl;
        transform(coll1.begin(),coll1.end(),
                        back_inserter(coll2),
                        bind2nd(multiplies<int>(),10));
        PrintElements(coll2,"coll2 bind2nd : ");

        std::cout<<"求反 : ";
        transform(coll2.begin(),coll2.end(),
                        ostream_iterator<int>(cout," "),
                        negate<int>());
    }

    void Learn_swap_ranges()
    {
        std::cout<<std::endl<<"--------------------in Learn swap_ranges()"<<endl;

        vector<int> coll1;
        list<int> coll2;

        InsertElements(coll1,1,9);
        InsertElements(coll2,20,35);
        PrintElements(coll1,"coll1 : ");
        PrintElements(coll2,"coll2 : ");

        std::cout<<"交换 : "<<std::endl;
        list<int>::iterator pos;
        pos = swap_ranges(coll1.begin(),coll1.end(),coll2.begin());

        PrintElements(coll1,"coll1 : ");
        PrintElements(coll2,"coll2 : ");
        if(pos != coll2.end())
        {
            std::cout<<"第一个没有交换的元素是:"<<*pos<<std::endl;
        }

        std::cout<<"第一个元素"<<*coll2.begin() <<"和最后一个元素"<<*coll2.rbegin()<<"互换:"<<std::endl;
        swap_ranges(coll2.begin(),++coll2.begin(),coll2.rbegin());
        PrintElements(coll2,"coll2 : ");
    }

    void Learn_fill()
    {
        std::cout<<std::endl<<"--------------------in Learn fill() fill_n()"<<endl;

        list<string> coll;

        std::cout<<"填充10次‘hello’ : "<<std::endl;
        fill_n(back_inserter(coll),10,"hello");
        PrintElements(coll,"coll : ");

        std::cout<<"coll用world填充 : "<<std::endl;
        fill(coll.begin(),coll.end(),"world");
        PrintElements(coll,"coll : ");

        std::cout<<"填充8次‘hello’ : "<<std::endl;
        fill_n(coll.begin(),8,"hello");
        PrintElements(coll,"coll : ");
    }

    void Learn_generate()
    {
        std::cout<<std::endl<<"--------------------in Learn generate() generate_n()"<<endl;

        list<int> coll;

        std::cout<<"赋值 10次随机值 : "<<std::endl;
        generate_n(back_inserter(coll),10,rand);
        PrintElements(coll,"coll : ");

        std::cout<<"赋随机值 : "<<std::endl;
        generate(coll.begin(),coll.end(),rand);
        PrintElements(coll,"coll : ");
    }

    void Learn_replace()
    {
        std::cout<<std::endl<<"--------------------in Learn replace() replace_if() replace_copy() replace_copy_if()"<<endl;

        list<int> coll;
        list<int> coll2;

        InsertElements(coll,2,7);
        InsertElements(coll,5,9);
        PrintElements(coll,"coll : ");

        std::cout<<"值为6的元素替换为1: "<<std::endl;
        replace(coll.begin(),coll.end(),
                    6,
                    1);
        PrintElements(coll,"coll : ");

        std::cout<<"小于5的元素替换为0: "<<std::endl;
        replace_if(coll.begin(),coll.end(),
                        bind2nd(less<int>(),5),
                        0);
        PrintElements(coll,"coll : ");

        std::cout<<"拷贝coll到coll2,拷贝过程中把5替换为55: "<<std::endl;
        replace_copy(coll.begin(),coll.end(),
                        back_inserter(coll2),
                        5,
                        55);
        PrintElements(coll,"coll : ");
        PrintElements(coll2,"coll2 : ");

        std::cout<<"拷贝coll到coll2,拷贝过程中把小于5的替换为11: "<<std::endl;
        replace_copy_if(coll.begin(),coll.end(),
                        back_inserter(coll2),
                        bind2nd(less<int>(),5),
                        11);
        PrintElements(coll,"coll : ");
        PrintElements(coll2,"coll2 : ");
    }

    int main()
    {
        cout<<"hello world"<<endl;

        //--------------------变动性算法--------begin
        Learn_for_each2();
        Learn_copy();
        Learn_transforming();
        Learn_swap_ranges();
        Learn_fill();
        Learn_generate();
        Learn_replace();
        //cin.get();

        return 0;
    }
  • 相关阅读:
    8常用控件
    7对话框
    6控件
    5Lambda表达式
    4自定义信号和槽函数
    3信号与槽
    2指定父对象
    springboot整合activemq
    springboot整合springtask
    jvm与tomcat启动优化配置
  • 原文地址:https://www.cnblogs.com/ningth/p/2359426.html
Copyright © 2020-2023  润新知