#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的问题的更多相关文章

  1. std::remove_if

    原型: #include <algorithm>forward_iterator remove_if( forward_iterator start, forward_iterator e ...

  2. C++ count_if/erase/remove_if 用法详解

    每次使用这几个算法时都要去查CPP reference,为了能够加深印象,整理一下基本应用. cout/cout_if:  return the number of elements satisfyi ...

  3. STL --> remove和remove_if()

    remove和remove_if() 一.Remove()函数 remove(beg,end,const T& value) //移除区间{beg,end)中每一个“与value相等”的元素: ...

  4. 移除元素(remove,remove_if...unique...)

    remove 因为本算法作用的是iterator,所以并不会改变Container大小,会返回一个新的iterator new_last,是的first到new_last中的元素都不等于value,左 ...

  5. 【C++】STL算法之remove_if

    之前写过这样一段代码: auto iter=remove_if(AllEdges.begin(),AllEdges.end(),[&](Edge* edge){return _isEedge( ...

  6. c++ remove_if

    #include <algorithm> 函数remove_if()移除序列[start, end)中所有应用于谓词p返回true的元素. 此函数返回一个指向被修剪的序列的最后一个元素迭代 ...

  7. C++之remove和remove_if

    一.Remove()函数 remove(beg,end,const T& value) //移除区间{beg,end)中每一个“与value相等”的元素: remove只是通过迭代器的指针向前 ...

  8. erase & remove_if 合用

    words_.erase( remove_if( words_.begin(), words_.end(), [&](const entry& e) { return (e.type ...

  9. C++ remove remove_if erase

    #include <iostream>#include <algorithm>#include <list>#include <vector>#incl ...

随机推荐

  1. 老男孩Python高级全栈开发工程师【真正的全套完整无加密】

    点击了解更多Python课程>>> 老男孩Python高级全栈开发工程师[真正的全套完整无加密] 课程大纲 老男孩python全栈,Python 全栈,Python教程,Django ...

  2. 译文 编写一个loader

    https://doc.webpack-china.org/contribute/writing-a-loader loader是一个导出了函数的node模块,当资源须要被这个loader所转换的时候 ...

  3. vue源码构建代码分析

    这是xue源码学习记录,如有错误请指出,谢谢!相互学习相互进步. vue源码目录为 vue ├── src #vue源码 ├── flow #flow定义的数据类型库(vue通过flow来检测数据类型 ...

  4. asyn_fifo

    //Module Name:afifo_ctrl //Description:parameterized afifo module afifo_ctrl( clk_push, rst_push_n, ...

  5. 【cpu】CPU版本认识

    此图应该是摘选自<鸟哥的linux私房菜>

  6. pytorch导入错误so: undefined symbol: _Z11libshm_initPKc

    首先删除torch文件 或者直接卸载 删除会更彻底 https://blog.csdn.net/qq_37674858/article/details/88870124 但是会发现卸载重装pytorc ...

  7. SQL Server on Red Hat Enterprise Linux

    本文从零开始一步一步介绍如何在Red Hat Enterprise Linux上搭建SQL Server 2017,包括安装系统.安装SQL等相关步骤和方法(仅供测试学习之用,基础篇). 一.   创 ...

  8. CodeIgniter实现读写分离

    http://pengbotao.cn/codeigniter-mysql-proxy.html

  9. ImportError: No module named ‘MySQLdb'

    1.APP未注册 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contentty ...

  10. excel设置单元格为文本

    可以使用分裂功能,解决单元格无法设置成文本的问题.