Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   question about function object (http://www.go4expert.com/forums/question-function-object-t25000/)

rpbear 16Feb2011 14:52

question about function object
 
I have a code as following:
Code:

#include <iostream>
#include <algorithm>
#include <list>
using namespace std;

//the class Nth is a predicates
class Nth{
    private:
        int nth;
        int count;
    public:
        Nth(int n):nth(n),count(0){}
        bool operator()(int){
            return ++count == nth;
        }
};

int main()
{
      list<int> lst;
 
      for(int i=1;i<10;i++)
          lst.push_back(i);

      copy(lst.begin(),lst.end(),ostream_iterator<int>(cout," "));
      cout<<endl;

      list<int>::iterator pos = remove_if(lst.begin(),lst.end(),Nth(3));
      lst.erase(pos,lst.end());

      copy(lst.begin(),lst.end(),ostream_iterator<int>(cout," "));
      cout<<endl;
}

the output is:
1 2 3 4 5 6 7 8 9
1 2 4 5 7 8 9
Not as I expect:
1 2 3 4 5 6 7 8 9
1 2 4 5 6 7 8 9
The book explained:
This happens because the usual implementation of the algorithm copies the predicate internally during the algorithm:
Code:

template<class ForwIter,class Predicate>
ForwIter std::remove_if(ForwIter beg,ForeIter end,Predicate op)
{
      ForwIter pos = find_if(beg,end,op);
      if(pos == end)
            return beg;
      else{
            ForwIter next = beg;
            return remove_copy_if(++next,end,beg,op);
      }
}

and it says it is possible to avoid this surprise behavior like this:
Code:

template<class ForwIter,class Predicate>
ForwIter std::remove_if(ForwIter beg,ForeIter end,Predicate op)
{
      while(beg != end && !op(*beg))
            beg++;
      if(pos == end)
            return beg;
      else{
            ForwIter next = beg;
            return remove_copy_if(++next,end,beg,op);
      }
}

I don't know what the difference between the two implementations is.I think i was able to understand the first one,but why the second one works fine?I thought they were the same.Any help will be appreciated.


All times are GMT +5.5. The time now is 18:40.