#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. sklearn 快速入门教程

    1. 获取数据 1.1 导入sklearn数据集 sklearn中包含了大量的优质的数据集,在你学习机器学习的过程中,你可以通过使用这些数据集实现出不同的模型,从而提高你的动手实践能力,同时这个过程也 ...

  2. 图像分割loss集合

    我们只是大佬的搬运工 1.log loss 2.WBE loss 带权重的交叉熵 3.Focal loss 容易过拟合?我在VGG16上做过实验(没有BN层),发现网络在训练集上的性能直线上升,但是验 ...

  3. timer event

    /* linux/kernel/time/jiffies.c*/ static cycle_t jiffies_read(struct clocksource *cs) { return (cycle ...

  4. Codeforces Round #439 (Div. 2) A. The Artful Expedient

    A. The Artful Expedient 题目链接http://codeforces.com/contest/869/problem/A 解题心得:就是一个水题,读懂题就好,题意是,(i,j)a ...

  5. 创建安卓模拟器的两种方式及常用Android命令介绍

    创建安卓模拟器有以下两种方式: 1>通过图形界面创建,在Eclipse中单击Windows->Android Virtual Device Manager启动图形界面窗口 2>如果用 ...

  6. MFC中的CListControl控件

    一直想要这种效果,无奈刚开始用了cListbox控件,不知道怎么生成背景的边框,找了好久资料,突然发现好像控件用错了. 用CListControl控件实现图中效果,超级开心~ 实现过程: 添加一个Li ...

  7. ios sqlite 简单使用

    // // ViewController.m // sqlitedemo // // Created by lam_TT on 15-4-11. // Copyright (c) 2015年 lam_ ...

  8. 常州模拟赛d4t3 字符串划分

    题目描述 给你一串由小写字母组成的字符串,希望你把它划分成一些小段,使得每一小段字符串中的字母 都不相同,并且希望分的段数尽量少. 然后,把这些小段按字典序排序后输出,中间由一个空格分隔. 例如:字符 ...

  9. 【dp】leetcode Best Time to Buy and Sell Stock IV

    https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/ [题意] 给定n天股市的票价,最多交易k次, ...

  10. 【2018.10.20】noip模拟赛Day3 飞行时间

    今天模拟赛题目 纯考输入的傻逼题,用$scanf$用到思想僵化的我最终成功被$if$大法爆$0$了(这题只有一组$100$分数据). 输入后面那个$(+1/2)$很难$if$判断,所以我们要判两个字符 ...