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

C++ STD Gems02的更多相关文章

  1. 【NX二次开发】NX内部函数,libuifw.dll文件中的内部函数

    本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: void U ...

  2. C++ std::set

    std::set template < class T, // set::key_type/value_type class Compare = less<T>, // set::k ...

  3. C++ std::priority_queue

    std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...

  4. C++ std::queue

    std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...

  5. C++ std::multimap

    std::multimap template < class Key, // multimap::key_type class T, // multimap::mapped_type class ...

  6. C++ std::map

    std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...

  7. C++ std::list

    std::list template < class T, class Alloc = allocator > class list; List Lists are sequence co ...

  8. C++ std::forward_list

    std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...

  9. C++ std::deque

    std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...

随机推荐

  1. kali linux 添加源 及为firefox esr 添加 flash 插件 --2017

    终端 输入 vim /etc/apt/sources.list 在文件内写入 #中科大deb http://mirrors.ustc.edu.cn/kali kali-rolling main non ...

  2. MyBatis笔记(一)

    MyBatis学习随笔 第一个MyBatis项目 创建项目 导入maven依赖,根据需要选择性添加mysql/oracle,spring,spring-mybatis等依赖,这里就不一一列出了 < ...

  3. Java 类加载器(ClassLoader)

    类加载器 ClassLoader 什么是类加载器? 通过一个类的全限定名来获取描述此类的二进制字节流这个动作放到Java虚拟机外部去实现, 以便让应用程序自己决定如何去获取所需要的类.实现这个动作的代 ...

  4. java时间差

    以下代码没什么意义,只是记录 long mstart = System.nanoTime(); int mIndex = 0 ; for (int i = 0; i < 100000000; i ...

  5. 四十四、在SAP中冻结第一行表头

    一.表格数据量大了,如果需要界面滚动,则看不到第一行的表头文本 二.代码如下: 二.效果如下,任意滚动,表头还是被冻结可以看到

  6. 168-PHP 输出系统当前时间

    <body style="font-family:'华文彩云'; color:#0000CC; font-size:20px"> 系统的当前时间是: <?php ...

  7. 报错SQL盲注之BIGINT 溢出

    首先感谢原创博主,在此致敬.本文转自:http://www.cnblogs.com/lcamry/articles/5509112.html MySQL版本在 5.5.5 及其以上 0x01 概述 我 ...

  8. oracle将字符串根据特定字符串拆分为多个子字符串

    将 字符串 '20180321-4768-4735261' 按‘-’  拆分: 语法: INSTR()函数 1.用处: 在一个字符串中查找指定的字符,返回被查找到的指定的字符的位置. 2.语法格式: ...

  9. linux常用命令-关机、重启

    常用命令-关机.重启 命令 含义 reboot 重新启动操作系 shutdown –r now 重新启动操作系统,shutdown会给别的用户提示 shutdown -h now 立刻关机,其中now ...

  10. POJ 1028:Web Navigation

    Web Navigation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30828   Accepted: 13821 ...