C++ STD Gems02

remove、remove_if、replace、replace_if、remove_copy_if、unique

#include 
#include 
#include 
#include 
#include 
#include 

template
void write_to_cout(const Container& container, const char* delimiter = " ")
{
    std::copy(container.begin(), container.end(),
        std::ostream_iterator(std::cout, delimiter) );
}

template
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 a = {"zero", "one", "two", "three", "four", "five", "six", "seven"};
    //std::vector 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 a = {"zero", "one", "two", "three", "four", "five", "six", "seven"};
    std::vector 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 a = {"zero", "one", "two", "three", "four", "five", "six", "seven"};
    std::vector 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 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 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;
}

你可能感兴趣的:(C++ STD Gems02)