Follow-up from "Correct weights loop in storeInitialWeights in CaloFutureShowerOverlapTool"
The following discussion from !1304 (merged) should be addressed:
-
@graven started a discussion: Even nicer (IMO) would be to define a small, generic, helper function for this:
#include <functional> #include <utility> namespace { template <typename... MapArgs, typename Predicate> void erase_if( std::map<MapArgs...>& map, Predicate&& predicate ) { auto i = map.begin(); auto end = map.end(); // std::map::erase does not invalidate end -- so it can be cached while ( i != end ) { if (std::invoke(predicate,std::as_const(*i))) i = map.erase(i); else ++i; } } }
and use it here as:
erase_if( weights, [](const auto& w) { return w.second == 1. ; } // warning: floating point equality is unlikely to yield 'true'...
This moves the 'boring details' of how to properly erase things from a map out of the body of the 'interesting' code, making it easier to read ;-)
And once we get to C++20, one can use
std::erase_if
(see here) for this.