Just create an at()
function which mimicks what C++11 std::map<...>::at()
does:
template <typename K, typename V, typename C, typename A>
V const& at(std::map<K, V, C, A> const& m, K const& k) {
typename std::map<K, V, C, A>::const_iterator it(m.find(k));
if (it == m.end()) {
throw std::out_of_range("key not found in map");
}
return it->second;
}
Note that calling at()
in each iteration of a loop is a Bad Idea! Searching a std::map<...>
is efficient in the theoretical sense but that doesn't mean that it is fast in practice! You are much better off to search the relevant node just one and then keep using it.