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   yi_meng  阅读(340)  评论(0)    收藏  举报

< 2025年5月 >
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7

导航

统计

点击右上角即可分享
微信分享提示