#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. auth认证组件

    Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Djang ...

  2. (转)iOS开发之Pch预编译文件的创建

    本文转自 http://www.cnblogs.com/496668219long/p/4568265.html 在Xcode6之前,创建一个新工程xcode会在Supporting files文件夹 ...

  3. 查看zookeeper管理的solrcloud配置文件

    进入zookeeper/bin目录下 连接zookeeper sh zkCli.sh -server localhost:2181 查看 ls /configs 结果如下 [zk: localhost ...

  4. 和为s的两个数字 和为s的连续正数序列

    输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s,如果有多对数字的和等于s,输出任意一对即可. #include <iostream> using namesp ...

  5. PAT Basic 1056

    1056 组合数的和 给定 N 个非 0 的个位数字,用其中任意 2 个数字都可以组合成 1 个 2 位的数字.要求所有可能组合出来的 2 位数字的和.例如给定 2.5.8,则可以组合出:25.28. ...

  6. django的rest framework框架——版本、解析器、序列化

    一.rest framework的版本使用 1.版本可以写在URL中,通过GET传参,如 http://127.0.0.1:8082/api/users/?version=v1 (1)自定义类获取版本 ...

  7. STM32F407 I2C 个人笔记

    源代码;https://github.com/YuQiao0303/STM32F407-Examples/tree/master/24.IIC 概述 I2C (IIC, Inter-Integrate ...

  8. USACO Longest Prefix

    题目大意:给出一个长字符串,问最长的前缀,使得这个前缀能用给出的一些元素组合而成 思路:暴力dp,dp[i]表示长度为i的前缀能否被表示 /*{ ID:a4298442 PROB:prefix LAN ...

  9. BZOJ 3750: [POI2015]Pieczęć 【模拟】

    Description 一张n*m的方格纸,有些格子需要印成黑色,剩下的格子需要保留白色. 你有一个a*b的印章,有些格子是凸起(会沾上墨水)的.你需要判断能否用这个印章印出纸上的图案.印的过程中需要 ...

  10. 【最优K叉树】hdu 5884 Sort

    http://acm.hdu.edu.cn/showproblem.php?pid=5884 参考:https://www.cnblogs.com/jhz033/p/5879452.html [题意] ...