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;
}
C++ STD Gems02的更多相关文章
- 【NX二次开发】NX内部函数,libuifw.dll文件中的内部函数
本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: void U ...
- C++ std::set
std::set template < class T, // set::key_type/value_type class Compare = less<T>, // set::k ...
- C++ std::priority_queue
std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...
- C++ std::queue
std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...
- C++ std::multimap
std::multimap template < class Key, // multimap::key_type class T, // multimap::mapped_type class ...
- C++ std::map
std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...
- C++ std::list
std::list template < class T, class Alloc = allocator > class list; List Lists are sequence co ...
- C++ std::forward_list
std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...
- C++ std::deque
std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...
随机推荐
- Java集合基于JDK1.8的ArrayList源码分析
本篇分析ArrayList的源码,在分析之前先跟大家谈一谈数组.数组可能是我们最早接触到的数据结构之一,它是在内存中划分出一块连续的地址空间用来进行元素的存储,由于它直接操作内存,所以数组的性能要比集 ...
- thinkphp5+python.apscheduler实现计划任务
1.thinkphp5配置自定义命令行 /application/console/command namespace app\console\command; use think\console\Co ...
- python基础学习(一)
一,Python介绍 1,python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打 ...
- android studio3.1 添加闪屏页面(启动欢迎界面)(例子简单无BUG)
截图 启动页的 activity_splash.xml 我用了一张图片自己添加吧 <?xml version="1.0" encoding="utf-8" ...
- Codeforces Round #619 (Div. 2)
A. Three Strings 题意:给三个长度相同的非空字符串abc,依次将c中的每个字符和a或者b中对应位置的字符进行交换,交换必须进行,问能否使得ab相同. 思路:对于每一个位置,如果三个字符 ...
- mjpg-streamer视频服务器移植
相关软件下载地址:http://pan.baidu.com/s/16yo8Y JPEG库的移植 对于内核linux-2.6.35,不能再使用servfox,如果要使用的需要修改大量的代码.在此选用新的 ...
- Ubuntu14.04 无法关机 SpamAssassin speech-dispatcher
在ubuntu14.04上安装完一些包后,关闭计算机就会出现关于标题中的两个错误. 1.在软件中心卸载spamAssassin 2.运行命令: sudo update-rc.d -f speech-d ...
- ACM-奇特的立方体
题目描述:奇特的立方体 任意给出8个整数,将这8个整数分别放在一个立方体的八个顶点上,要求检验每个面上的四个数之和相等这个条件能否被满足. 输入 一次输入8个整数 输出 YES或者NO YES表示可能 ...
- poj 1330 Nearest Common Ancestors 求最近祖先节点
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37386 Accept ...
- (四)requests模块的cookies和代理操作
基于requests模块的cookie操作 引言:有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取某个人“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到 ...