remove_if的问题
#include<iostream>
#include<list>
#include<algorithm>
#include"PRINT_ELEMENTS.h" using namespace std; class Nth{
private:
int nth;
int count;
public:
Nth(int n):nth(n),count(){
}
bool operator()(int){
return ++count==nth;
}
}; int main(int argc, char **argv)
{
list<int> coll;
for(int i=; i<=;++i){
coll.push_back(i);
}
PRINT_ELEMENTS(coll,"initialized: "); list<int>::iterator pos = remove_if(coll.begin(),coll.end(),Nth());
coll.erase(pos, coll.end());
PRINT_ELEMENTS(coll, "remove_if: ");
return ;
}
//PRINT_ELEMENTS.h
template<typename T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr << std::endl;
for(pos = coll.begin(); pos != coll.end(); ++pos)
std::cout << *pos << " ";
std::cout << std::endl;
}
输出结果:
initialized:
remove_if:
而不是:
initialized:
remove_if:
此处是remove_if的问题:
remove_if源代码为:
template<typename ForwIter, typename Predicate>
ForwIter remove_if(ForwIter beg, ForwIter end, Predicate op)
{
beg=find_if(beg, end, op) if(beg == end)
{
return beg;
}
else
{
ForwIter next = beg;
return remove_copy_if(++next, end, beg, op);
}
}
这里find_if调用op是复制了一个op的副本,所以当remove_copy_if再次调用op是用重新计数了,所以也把6删除了;
可以用:
#include<iostream>
#include<list>
#include<algorithm>
#include"PRINT_ELEMENTS.h" using namespace std; class Nth{
private:
int nth;
int count;
public:
Nth(int n):nth(n),count(){
}
bool operator()(int){
return ++count==nth;
}
}; template<typename ForwIter, typename Predicate>
ForwIter remove_if_my(ForwIter beg, ForwIter end, Predicate op)
{
while(beg != end && !op(*beg))
{
++beg;
} if(beg == end)
{
return beg;
}
else
{
ForwIter next = beg;
return remove_copy_if(++next, end, beg, op);
}
}
int main(int argc, char **argv)
{
list<int> coll;
for(int i=; i<=;++i){
coll.push_back(i);
}
PRINT_ELEMENTS(coll,"initialized: "); list<int>::iterator pos = remove_if_my(coll.begin(),coll.end(),Nth());
coll.erase(pos, coll.end());
PRINT_ELEMENTS(coll, "remove_if: ");
return ;
}
这样避去用find_if,所以就可以了。
参考:《C++标准程序库》P302
remove_if的问题的更多相关文章
- std::remove_if
原型: #include <algorithm>forward_iterator remove_if( forward_iterator start, forward_iterator e ...
- C++ count_if/erase/remove_if 用法详解
每次使用这几个算法时都要去查CPP reference,为了能够加深印象,整理一下基本应用. cout/cout_if: return the number of elements satisfyi ...
- STL --> remove和remove_if()
remove和remove_if() 一.Remove()函数 remove(beg,end,const T& value) //移除区间{beg,end)中每一个“与value相等”的元素: ...
- 移除元素(remove,remove_if...unique...)
remove 因为本算法作用的是iterator,所以并不会改变Container大小,会返回一个新的iterator new_last,是的first到new_last中的元素都不等于value,左 ...
- 【C++】STL算法之remove_if
之前写过这样一段代码: auto iter=remove_if(AllEdges.begin(),AllEdges.end(),[&](Edge* edge){return _isEedge( ...
- c++ remove_if
#include <algorithm> 函数remove_if()移除序列[start, end)中所有应用于谓词p返回true的元素. 此函数返回一个指向被修剪的序列的最后一个元素迭代 ...
- C++之remove和remove_if
一.Remove()函数 remove(beg,end,const T& value) //移除区间{beg,end)中每一个“与value相等”的元素: remove只是通过迭代器的指针向前 ...
- erase & remove_if 合用
words_.erase( remove_if( words_.begin(), words_.end(), [&](const entry& e) { return (e.type ...
- C++ remove remove_if erase
#include <iostream>#include <algorithm>#include <list>#include <vector>#incl ...
随机推荐
- Mysql 随机函数 rand()
rand() 函数主要有两个用处: 1.是产生随机数, 2.是随机排序(在数据较大的时候会变成性能杀手) 实例: 1.产生一个随机数,默认0~1之间的浮点数 SELECT RAND( ) 2.参数指定 ...
- pycharm添加wordcloud模块时报错:error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
windows 7 32bit python3.6.3 32bit pycharm2018社区版 32bit 问题说明: 添加wordcloud模块时报错:error: Microsoft Visua ...
- python模块--random
random主要用于生成随机字符串等,例如登录页面上随机字符串验证. random常用方法: import random print(random.randrange(1, 10)) # 返回1-10 ...
- python基础学习笔记——os模块
#OS模块 #os模块就是对操作系统进行操作,使用该模块必须先导入模块: import os #getcwd() 获取当前工作目录(当前工作目录默认都是当前文件所在的文件夹) result = os. ...
- PYDay10&11&12&13-常用模块:time|datetime|os|sys|pickle|json|xml|shutil|logging|paramiko、configparser、字符串格式化、py自动全局变量、生成器迭代器
1.py文件自动创建的全局变量 print(vars()) 返回值:{'__name__': '__main__', '__package__': None, '__loader__': <_f ...
- 【03】const
[03]const 魔芋总结: 1,声明的是常量,一经声明,不得修改.必须声明的同时并赋值.否则报错. 2,只在声明所在的块级作用域内有效. 3,const命令声明的常量也是不提升,同样存在暂时性死区 ...
- Appium自动化-基于java的环境搭建
引言 自动化测试框架搭建主要分为以下几个方面的下载安装及环境配置: 1.jdk 2. adt 3. appium 4. testng插件 工具链接: https://pan.baidu.com/s/1 ...
- 【Android】android:ellipsize的使用以及一个点解决方法
EidtText和textview中内容过长的话自动换行,使用android:ellipsize与android:singleine可以解决,使只有一行. EditText不支持marquee 用法如 ...
- 刷题总结——射箭(bzoj2732)
题目: Description 沫沫最近在玩一个二维的射箭游戏,如下图 1 所示,这个游戏中的 x 轴在地面,第一象限中有一些竖直线段作为靶子,任意两个靶子都没有公共部分,也不会接触坐标轴.沫沫控制一 ...
- ecplise 使用快捷键
1. [ALT+/] 此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不全方法和属性名称犯愁,当记不全类.方法和属性的名字时,多体验一下[ALT+/]快捷键带来的好处吧. 2. ...