reference says that
template< class ForwardIt, class UnaryPredicate > ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p );
Iterators pointing to elements between the old and new ends of the range are still wanted, but the elements themselves are vague values.
I tried this simple program to find out what they mean by "unspecified values."
#include <vector> #include <memory> #include <iostream> #include <algorithm> int main() { std::vector< std::shared_ptr<int> > ints; for (int i = 0; i < 10; ++i) ints.push_back(std::make_shared<int>(i)); std::remove_if(ints.begin(), ints.end(), [](const std::shared_ptr<int>& element) { return *element % 7 != 0; }); for (int i = 0; i < 10; ++i) std::cout << *ints[i] << std::endl; return 0; }
Output:
0 7 2 3 4 5 6 The program has unexpectedly finished.
This is something mysterious happens to the data after the 7th element, which causes segfault.
Interestingly, a possible implementation from here
template<class ForwardIt, class UnaryPredicate> ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p) { ForwardIt result = first; for (; first != last; ++first) { if (!p(*first)) { *result++ = *first; } } return result; }
Does not create segfault.
This is mistake? Because iterators must be unsolvable. I am using gcc 4.7.3
source share