class Foo {
bool foo_;
public:
Foo(bool foo) : foo_(foo) {}
bool Bar() { return foo_; }
};
int main() {
std::vector<Foo*> foos;
foos.push_back(new Foo(false));
foos.push_back(new Foo(true));
foos.push_back(new Foo(true));
foos.push_back(new Foo(true));
for(std::vector<Foo*>::iterator i = foos.begin(); i != foos.end(); ++ i)
{
std::cout << "Bar() = " << (*i)->Bar() ? "true" : "false" << std::endl;
}
std::cout << std::endl;
struct IsBar {
bool operator() (Foo* foo) {
return foo->Bar();
}
};
std::remove_if(foos.begin(), foos.end(), IsBar());
for(vector<Foo*>::iterator i = foos.begin(); i != foos.end(); ++ i)
{
std::cout << "Bar() = " << (*i)->Bar() ? "true" : "false" << std::endl;
}
}
false
true
true
true
true
true
true
true
Shouldn't the second block of output end with false? remove_if moves elements that satisfy the predicate to the end of the range, correct? I'm really confused by this :(