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. 通过流量管理器和 Azure Functions(作为代理)为全球用户提供最靠近的认知服务(或自定义API)

    本实战是一个中等复杂度的综合性实战,涉及到的内容有TrafficManager,AzureFunctions,域名/域名解析等几个内容. 本案例基础介绍: https://www.bilibili.c ...

  2. 001.Oracle数据库 , 查询日期在两者之间

    /*Oracle数据库查询日期在两者之间*/ SELECT OCCUR_DATE FROM LM_FAULT WHERE ( ( OCCUR_DATE >= to_date( '2017-05- ...

  3. Git如何将本地test分支设置跟踪origin/test分支

    前提条件: 有一个远程仓库其中只有一个master分支,然后我把它clone到本地 1.新建一个本地分支test, 2.然后把它push到远程仓库(git push origin test) 3.本地 ...

  4. 【转帖】Windows 10版本占比一览:v1903依然最稳定 占比52.6%

    Windows 10版本占比一览:v1903依然最稳定 占比52.6% https://os.51cto.com/art/202002/611452.htm 其实感觉现阶段的win10 与 2015年 ...

  5. duilib+cef自定义浏览器控件编译错误

    新版博客已经搭建好了,有问题请访问 htt://www.crazydebug.com 公司二期好主播项目,决定用duilib开发界面,且从ie内核换成谷歌内核 再用duilib自定义一个Browser ...

  6. fiddler 限速方法

    1.使用的软件下载地址: \\192.168.100.2\共享软件\开发常用\flash_team\工作软件\fiddler2setup.exe 2.注意事项 测试是,在ie浏览器环境下测试 3.软件 ...

  7. C++学习链表

    #include"pch.h" #include<iostream> #include<string> using namespace std; struc ...

  8. Redis 详解 (五) redis的五大数据类型实现原理

    目录 1.对象的类型与编码 ①.type属性 ②.encoding 属性和 *prt 指针 2.字符串对象 3.列表对象 4.哈希对象 5.集合对象 6.有序集合对象 7.五大数据类型的应用场景 8. ...

  9. react的this.setState详细介绍

    this.setState是react类组件中最常用的一个react API,使用它可以改变state从而改变页面.今天我们就来详细的学习一下这个东西.比如: import React, { Comp ...

  10. eclipse中svn重新设置账户

    查看svn版本:windows > preference > Team > SVN 1.如果svn插件是svnkit版 只需找到.keyring文件,一般目录是:eclipse安装目 ...