remove_if equivalent for std::map

Almost. for(; iter != endIter; ) { if (Some Condition) { iter = aMap.erase(iter); } else { ++iter; } } What you had originally would increment the iterator twice if you did erase an element from it; you could potentially skip over elements that needed to be erased. This is a common algorithm I’ve seen … Read more

How to iterate an ArrayList inside a HashMap using JSTL?

You can use JSTL <c:forEach> tag to iterate over arrays, collections and maps. In case of arrays and collections, every iteration the var will give you just the currently iterated item right away. <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %> <c:forEach items=”${collectionOrArray}” var=”item”> Item = ${item}<br> </c:forEach> In case of maps, every iteration the var will give … Read more

Using char* as a key in std::map

You need to give a comparison functor to the map otherwise it’s comparing the pointer, not the null-terminated string it points to. In general, this is the case anytime you want your map key to be a pointer. For example: struct cmp_str { bool operator()(char const *a, char const *b) const { return std::strcmp(a, b) … Read more

How to remove from a map while iterating it?

The standard associative-container erase idiom: for (auto it = m.cbegin(); it != m.cend() /* not hoisted */; /* no increment */) { if (must_delete) { m.erase(it++); // or “it = m.erase(it)” since C++11 } else { ++it; } } Note that we really want an ordinary for loop here, since we are modifying the container … Read more