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

最新文章

  1. hibernate笔记--缓存机制之 二级缓存(sessionFactory)和查询缓存
  2. 删除 QQ 最新版右键菜单 通过QQ发送文件到手机
  3. 【MongoDB】2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061 由于目标计算机积极拒绝,无法连接。
  4. jenkins2 pipeline入门
  5. C#获取枚举描述代码
  6. Linux时间同步配置方法
  7. P1111 修复公路
  8. JQuery实现页面Loading效果
  9. Android_baseComponentExample
  10. Hbase region 某个regionserver挂掉后的处理
  11. (二): 基于ZeroMQ的实时通讯平台
  12. IE8中marquee不显示出滚动效果的解决办法
  13. weblogic 内存配置
  14. 浏览器兼容性问题——IE不支持却很实用的CSS属性Outline和Child
  15. hiho一下 第144周(机会渺茫)解题报告及拓展
  16. 关于spring事务
  17. 跟我学AngularJS:全局变量设置之value vs constant vs rootscope vs 服务[转]
  18. 列举一些有所帮助的blog和文章
  19. 递增和递减进度条CCProgressTimer
  20. Python3.2官方文档翻译--输出格式化

热门文章

  1. 云时代架构阅读笔记十一——数据库SQL优化
  2. 使用JavaScript实现一个简单的编译器
  3. Delphi IDFtp用法,包含断点续传
  4. 一百一十一、SAP的OO-ALV之五,显示ALV表格
  5. 十二、Sap的压缩类型p的使用方法
  6. 131-PHP子类可以访问父类public修饰的类成员
  7. 【机器学习实战学习笔记(2-2)】决策树python3.6实现及简单应用
  8. 14 SQLite数据库
  9. torch.utils.data.DataLoader 将数据按批次分成很多组,每次抛出一个小组
  10. 吴裕雄--天生自然C++语言学习笔记:C++ 模板