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;
    }
    
  • 相关阅读:
    PAT 1006 Sign In and Sign Out
    PAT 1004. Counting Leaves
    JavaEE开发环境安装
    NoSql数据库探讨
    maven的配置
    VMWARE 下使用 32位 Ubuntu Linux ,不能给它分配超过3.5G 内存?
    XCODE 4.3 WITH NO GCC?
    在苹果虚拟机上跑 ROR —— Ruby on Rails On Vmware OSX 10.7.3
    推荐一首让人疯狂的好歌《Pumped Up Kicks》。好吧,顺便测下博客园可以写点无关技术的帖子吗?
    RUBY元编程学习之”编写你的第一种领域专属语言“
  • 原文地址:https://www.cnblogs.com/codemeta-2020/p/12112244.html
Copyright © 2011-2022 走看看