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. python基础学习(一)

    一,Python介绍 1,python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打 ...

  2. Python连载61-tkinter三种布局

    一.pack布局举例 #pack布局案例 import tkinter baseFrame = tkinter.Tk() #以下代码都是创建一个组件,然后布局 btn1 = tkinter.Butto ...

  3. 京东云与AI 10 篇论文被AAAI 2020 收录,京东科技实力亮相世界舞台

    美国时间2月7-12日,AAAI 2020大会在纽约正式拉开序幕,AAAI作为全球人工智能领域的顶级学术会议,每年评审并收录来自全球最顶尖的学术论文,这些学术研究引领着技术的趋势和未来.京东云与AI在 ...

  4. LeetCode160 相交链表(双指针)

    题目: click here!!题目传送门 思路: 1.笨方法 因为如果两个链表相交的话,从相交的地方往后是同一条链表,所以: 分别遍历两个链表,得出两个链表的长度,两个长度做差得到n,然后将长的链表 ...

  5. css画布

    绘制基本图形 绘制直线 <style> .canvas{ } </style> <canvas id="myCanvas1" style=" ...

  6. 023-PHP常用数组函数

    <?php $colors = array("red", "green", "blue","yellow"); p ...

  7. jQuery原理系列-工具函数

    jquery源码中有很多精妙的实现,对于我们每天都在使用的东西,一定要知其原理,如果遇到不能使用jquery环境,也能自己封装原生的代码实现. 1.检测类型 众所周知typeof 不能用来检测数据,会 ...

  8. Codeforces 460C 二分结果+线段树维护

    发现最近碰到好多次二分结果的题目,上次多校也是,被我很机智的快速过了,这个思想确实非常不错.在正面求比较难处理的时候,二分结果再判断是否有效往往柳暗花明. 这个题目给定n个数字的序列,可以操作m次,每 ...

  9. PHP笔记02

    PHP数组 能够在一个变量存储多个值 取值用下标,下标从0开始计算 好处:在处理物品清单等大量相同属性的变量内容时可以集中存取 <?php $arr=array("like" ...

  10. C#获取刚插入的数据的id

    在开发程序中我们经常会遇到两个表或多个表关联同时插入数据的需求. 那么我们刚给主表插入一条数据,接着给副表插入数据时其中一个字段要存储与主表关联的id,那么我们该怎么获取刚插入的那条数据的id呢?   ...