zoukankan      html  css  js  c++  java
  • C++ STD Gems02

    remove、remove_if、replace、replace_if、remove_copy_if、unique

    #include <iostream>
    #include <string>
    #include <iterator>
    #include <vector>
    #include <algorithm>
    #include <cctype>
    
    template<class Container>
    void write_to_cout(const Container& container, const char* delimiter = " ")
    {
        std::copy(container.begin(), container.end(),
            std::ostream_iterator<typename Container::value_type>(std::cout, delimiter) );
    }
    
    template<class Container, typename Pred>
    void my_remove(Container& cont, Pred pred)
    {    
        const auto new_end = std::remove_if( cont.begin(), cont.end(), pred );     // 返回移除元素后新的迭代器位置,但容器容量大小并没有减少。
        cont.erase(new_end, cont.end());  // 后续处理
        
    }
    
    void test0()
    {
        std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six", "seven"};
        //std::vector<std::string> b = {"0", "1", "2", "3", "4", "5", "6", "7"};
    
        write_to_cout(a);
        std::cout << std::endl;
        //write_to_cout(b);
        //std::cout << std::endl;
    
        //test algorithm
        // const auto new_end = std::remove_if(a.begin(), a.end(), [](std::string s)
        //         {
        //             return std::count(s.begin(), s.end(), 'o') == 0;
        //         } 
        // );     // 返回移除元素后新的迭代器位置,但容器容量大小并没有减少。
        // a.erase(new_end, a.end());  // 后续处理
    
        //可以将上述操作封装成一个函数
        my_remove(a, [](std::string s){return std::count(s.begin(), s.end(), 'e') == 0;} ); //移除不含有字母e的单词
    
        write_to_cout(a, "|");
        std::cout << std::endl;
        std::cout << a.size() << std::endl;
    }
    
    
    void test1()
    {
        std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six", "seven"};
        std::vector<std::string> b = {"0", "1", "2", "3", "4", "5", "6", "7"};
    
        write_to_cout(a);
        std::cout << std::endl;
        write_to_cout(b);
        std::cout << std::endl;
    
        //test algorithm
        //将容器a中的元素有条件地移除并复制到容易b中,但a中的元素并没有改变
        // 区分copy_if与remove_copy_if,他们唯一的不同之处在一个在copy时满足指定的谓词,另一个不满足明确的谓词
        std::remove_copy_if(a.begin(), a.end(), std::back_inserter(b), [](std::string s) {return std::count(s.begin(), s.end(), 'o') == 0;});
    
        write_to_cout(b);
        std::cout << std::endl;
        write_to_cout(a);
        std::cout << std::endl;
        std::cout << a.size() << std::endl;
        std::cout << std::endl << std::endl;  
    }
    
    void test2()
    {
        std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six", "seven"};
        std::vector<std::string> b = {"0", "1", "2", "3", "4", "5", "6", "7"};
    
        write_to_cout(a);
        std::cout << std::endl;
         write_to_cout(b);
         std::cout << std::endl;
    
        //test algorithm
        //测试一下remove_copy
        std::copy(a.begin(), a.begin() + 4, a.begin() + 3);
        std::remove_copy(b.begin(), b.begin() + 3, b.begin() + 3, "1");
    
        write_to_cout(a);
        std::cout << std::endl;
        write_to_cout(b);
        std::cout << std::endl << std::endl;
    }
    
    //replace_if
    void test3()
    {
        std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six", "seven"};
    
        write_to_cout(a);
        std::cout << std::endl;
    
        //test algorithm
        // 把容器中单词不含e的替换成11
        std::replace_if(a.begin(), a.end(), [](const std::string& s){return std::count(s.begin(), s.end(), 'e') == 0;}, "11");
        write_to_cout(a);
        std::cout << std::endl << std::endl;
    }
    
    void test4()
    {
            std::vector<std::string> b = {"0", "1", "1", "2", "2", "2", "3", "4", "5", "6", "7",};
            
            write_to_cout(b);
            std::cout << std::endl;
    
            //test algorithm
            // 对于无序的容器,需要先排序
            auto new_end = std::unique(b.begin(), b.end()); // 去除重复元素
            write_to_cout(b);
            std::cout << std::endl;
    }
    
    int main()
    {
        test0();
        test1();
        test2();
        test3();
        test4();
    
        return 0;
    }
    
  • 相关阅读:
    笔试助攻题(思路)
    const 修饰成员函数 前后用法(effective c++ 03)
    UNIX 是啥?!和Linux什么关系?
    我的offer之路(一)
    我的offer之路(一)
    ANSI C 与 K&R C
    c内置数据类型
    预处理器
    小数用二进制如何表示
    C++中有三种创建对象的方法
  • 原文地址:https://www.cnblogs.com/codemeta-2020/p/12112244.html
Copyright © 2011-2022 走看看