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 ...
随机推荐
- python 字典复制(存疑)
import copy x = {'a':1,'b':[2,3,4]} y = x.copy() z = copy.deepcopy(x) print(x) print(y) print(z) pri ...
- Java线程池 ThreadPoolExecutor类
什么是线程池? java线程池是将大量的线程集中管理的类, 包括对线程的创建, 资源的管理, 线程生命周期的管理. 当系统中存在大量的异步任务的时候就考虑使用java线程池管理所有的线程, 从而减少系 ...
- Django(二) 模板:基本使用、模板语法、模板继承
一.模板的使用实战 接:https://blog.csdn.net/u010132177/article/details/103788677 参考:https://docs.djangoproject ...
- TBLASTN
TBLASTN search translated nucleotide databases using a protein query
- flask部署:Ubuntu下使用nginx+uwsgi+supervisor部署flask应用
之前一直用的Centos或者Red hat,自从使用Ubuntu后,发现Ubuntu使用起来更方便,自此爱上Ubuntu. 一.从github上下载flask应用 1.我已经成功将自己编写好的应用上传 ...
- STM32初始
.安装软件.驱动 JLINK驱动.PL2303驱动.MDK4.7(装完破解) .源码编译完要用到的工具 烧录工具MCUISP.串口调试助手 .KEIL建工程模板(2种) (1)寄存器开发:工程文件夹下 ...
- Redis 详解 (四) redis的底层数据结构
目录 1.演示数据类型的实现 2.简单动态字符串 3.链表 4.字典 5.跳跃表 6.整数集合 7.压缩列表 8.总结 上一篇博客我们介绍了 redis的五大数据类型详细用法,但是在 Redis 中, ...
- Emacs: 设置窗口标题格式
Emacs默认的窗口标题常常比较简单,例如我的默认标题是"emacs@ubuntu".如果想要在标题中显示更多的信息,我们可以对它进行一些设置.由于Emacs的可扩展性,这种修改实 ...
- JavaScript的函数和对象介绍
一.JavaScript中的函数 1.函数的概述 JavaScript中的函数是一段可执行代码的合集,在需要执行的时候可以在方法名之后添加一对小括号执行方法.是一段可执行的字符串. 2.函数中隐藏的属 ...
- ROS2学习日志:TurtleSim测试日志(基于ROS2 Eloquent Elusor)
TurtleSim测试日志(基于ROS2 Eloquent Elusor) 1.ros2 run 1.1 ros2 run turtlesim turtlesim_node --ros-args -- ...