I'm looking at the example for the function object predicate. Here is the example that show what you should not do with the predicate
Code:#include <iostream> #include <list> #include <algorithm> #include <iterator> using namespace std; class Nth { private: int nth; int count; public: Nth ( int n) : nth(n) , count(0) {} bool operator() (int) // need in input, empty input field yield errors { return ++count == nth; } }; int main() { list<int> coll; // insert elements from 1 to 9 for ( int i = 0 ; i <= 9 ; ++i) { coll.push_back(i); } copy(coll.begin(),coll.end(), ostream_iterator<int>(cout," ") ); cout << endl; list<int>::iterator pos; pos = remove_if(coll.begin(),coll.end(),Nth(3)); coll.erase(pos,coll.end()); copy(coll.begin(),coll.end(), ostream_iterator<int>(cout," ") ); pos = find_if(coll.begin() , coll.end() , Nth(3)); cout << endl; cout << *pos << endl; cout << endl; }Code:// output 1 2 3 4 5 6 7 8 9 10 1 2 4 5 7 8 9 10
The author didn't explain it perfectly clear how that function fails. He only mentioned the use of find_if in the template of remove_if. But that wouldn't have cause the problem, would it?
I thought it gives the wrong answer because of using remove_copy_if after the use of find_if becuase remove_copy_if uses another copy of the predicate to move the rest of the range.
Am I on the right track here?



LinkBack URL
About LinkBacks


