remove_if的问题

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(0){
        }
        bool operator()(int){
            return ++count==nth;
        }
};

int main(int argc, char **argv)
{
    list<int> coll;
    for(int i=1; i<=9;++i){
        coll.push_back(i);
    }
    PRINT_ELEMENTS(coll,"initialized: ");
    
    list<int>::iterator pos = remove_if(coll.begin(),coll.end(),Nth(3));
    coll.erase(pos, coll.end());
    PRINT_ELEMENTS(coll, "remove_if: ");
    return 0;
}

 //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:
1 2 3 4 5 6 7 8 9
remove_if:
1 2 4 5 7 8 9

而不是:

initialized:
1 2 3 4 5 6 7 8 9
remove_if:
1 2 4 5 6 7 8 9

此处是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(0){
        }
        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=1; i<=9;++i){
        coll.push_back(i);
    }
    PRINT_ELEMENTS(coll,"initialized: ");
    
    list<int>::iterator pos = remove_if_my(coll.begin(),coll.end(),Nth(3));
    coll.erase(pos, coll.end());
    PRINT_ELEMENTS(coll, "remove_if: ");
    return 0;
}

这样避去用find_if,所以就可以了。

 

参考:《C++标准程序库》P302

posted on 2013-11-19 10:36 yi_meng 阅读(...) 评论(...) 编辑 收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值