• 【c++算法】移除性算法


    根据元素值或某一条件,在区间内移除某些元素

     

    Remove

    将等于某特定值得元素全部移除

    Remove_if

    将满足条件的元素全部移除

    Remove_copy

    将不等于某一特定值得元素全部复制到它处

    Remove_copy_if

    将不满足条件的元素全部复制到它处

    Unique

    移除比邻的重复数据

    Unique_copy

    移除比邻的重复数据,并复制到它处


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

    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 << " ";
    }

    void Learn_remove()
    {
        std::cout<<endl<<"--------------------in Learn remove() remove_if()"<<endl
                                 <<"                            in Learn remove_copy() remove_copy_if()"<<endl
                                 <<"                            in Learn unique() unique_copy()"
                                 <<std::endl;

        vector<int> coll;
        InsertElements(coll,2,6);
        InsertElements(coll,4,9);
        InsertElements(coll,1,7);
        PrintElements(coll,"coll : ");
        PrintElementsPos(coll,"coll : ");

        vector<int>::iterator pos;
        std::cout<<"remove是逻辑上的删除,将被删除的元素移动到容器末尾"<<endl;
        pos = remove(coll.begin(),coll.end(),5);
        PrintElements(coll,"coll : ");
        PrintElementsPos(coll,"coll : ");
        std::cout<<"pos : " << *pos<<endl;
        std::cout<<"erase一般作为一个container的成员函数,是真正删除的元素,是物理上的删除"<<endl;
        coll.erase(pos,coll.end());
        PrintElements(coll,"coll erase : ");

        std::cout<<"移除小于4的元素 : " <<endl;
        coll.erase(remove_if(coll.begin(),coll.end(),
                                       bind2nd(less<int>(),4)),
                                       coll.end());
        PrintElements(coll,"coll : ");

        std::cout<<"使用remove_copy移除值为4的元素拷贝到cout : " <<endl;
        remove_copy(coll.begin(),coll.end(),
                            ostream_iterator<int>(cout," "),
                            4);
        std::cout<<endl;

        std::cout<<"使用remove_copy_if移除值大于7的元素拷贝到cout : " <<endl;
        remove_copy_if(coll.begin(),coll.end(),
                            ostream_iterator<int>(cout," "),
                            bind2nd(greater<int>(),7));
        std::cout<<endl;

        int source[] = {1,4,4,6,1,2,2,3,1,6,6,6,6,5,7,5,4,4};
        int count = sizeof(source)/sizeof(source[0]);
        coll.clear();

        copy(source,source+count,back_inserter(coll));
        PrintElements(coll,"coll init : ");

        std::cout<<"移除与前一个值相等的元素,逻辑移除"<<endl;
        pos = unique(coll.begin(),coll.end());
        PrintElements(coll,"coll : ");
        coll.erase(pos,coll.end());
        PrintElements(coll,"coll erase : ");

        coll.clear();
        copy(source,source+count,back_inserter(coll));
        PrintElements(coll,"coll init : ");
        std::cout<<"移除比前一个值小的元素,逻辑移除"<<endl;
        pos = unique(coll.begin(),coll.end(),greater<int>());
        PrintElements(coll,"coll : ");
        coll.erase(pos,coll.end());
        PrintElements(coll,"coll erase : ");

        std::cout<<"使用unique_copy拷贝到cout : " <<endl;
        unique_copy(coll.begin(),coll.end(),
                            ostream_iterator<int>(cout," ")
                            );
        std::cout<<endl;

        coll.clear();
        copy(source,source+count,back_inserter(coll));
        PrintElements(coll,"coll init : ");
        std::cout<<"移除比前一个值小的元素,拷贝到cout : " <<endl;
        unique_copy(coll.begin(),coll.end(),
                            ostream_iterator<int>(cout," "),
                            greater<int>());
        std::cout<<endl;
    }

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

        //--------------------移除性算法--------begin
        Learn_remove();
        //cin.get();

        return 0;
    }
  • 相关阅读:
    ERROR 1045 (28000): Access denied for user 'xxx'@'localhost' (using password: YES)
    --skip-grant-tables命令
    mysql之mysql_config_editor
    Mysql8报错:ERROR 1227 (42000): Access denied; you need (at least one of) the SYSTEM_USER privilege(s) for this operation
    创建仅有查询指定表权限的用户
    centos7修改分辨率
    CreateProcess简单示例
    光标在Edit控件中的位置
    设计模式:原型模式(C++)【克隆羊多莉】
    设计模式2:策略模式工厂
  • 原文地址:https://www.cnblogs.com/ningth/p/2359992.html
Copyright © 2020-2023  润新知