...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <vector> class BadPredicate : public std::unary_function<int, bool> { size_t timesCalled; public: BadPredicate() : timesCalled(0) {} bool operator()(const int &) { return ++timesCalled == 3; } }; template <typename Iter> void print_container(Iter Bb, Iter Ee) { std::cout << "Contains: "; std::copy(Bb, Ee, std::ostream_iterator<decltype(*Bb)>(std::cout, " ")); std::cout << std::endl; } void f() { std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; print_container(v.begin(), v.end()); v.erase(std::remove_if(v.begin(), v.end(), BadPredicate()), v.end()); print_container(v.begin(), v.end()); } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <algorithm> #include <iostream> #include <iterator> #include <vector> template <typename Iter> void print_container(Iter Bb, Iter Ee) { std::cout << "Contains: "; std::copy(Bb, Ee, std::ostream_iterator<decltype(*Bb)>(std::cout, " ")); std::cout << std::endl; } void f() { std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; print_container(v.begin(), v.end()); int timesCalled = 0; v.erase(std::remove_if(v.begin(), v.end(), [timesCalled](const int &) mutable { return ++timesCalled == 3; }), v.end()); print_container(v.begin(), v.end()); } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <vector> class BadPredicate : public std::unary_function<int, bool> { size_t timesCalled; public: BadPredicate() : timesCalled(0) {} bool operator()(const int &) { return ++timesCalled == 3; } }; template <typename Iter> void print_container(Iter Bb, Iter Ee) { std::cout << "Contains: "; std::copy(Bb, Ee, std::ostream_iterator<decltype(*Bb)>(std::cout, " ")); std::cout << std::endl; } void f() { std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; print_container(v.begin(), v.end()); BadPredicate BPbp; v.erase(std::remove_if(v.begin(), v.end(), std::ref(BPbp)), v.end()); print_container(v.begin(), v.end()); } |
...