Opened 21 years ago
Last modified 15 years ago
#924 closed Feature Requests (invalid)
Convenience function for const map read — at Version 1
Reported by: | nobody | Owned by: | nobody |
---|---|---|---|
Milestone: | To Be Determined | Component: | None |
Version: | Boost 1.34.0 | Severity: | Optimization |
Keywords: | Cc: |
Description (last modified by )
It's really awkward to use an STL map when you have only a const reference to it, because you cannot use operator[] -- you have to use find, check if the value was found, etc. (The reason being that operator[] creates a new map entry initialized with the null value if it doesn't find the key.) It would be much more convenient to have a lookup convenience function that either throws an exception if the key doesn't exist, or returns a default-initialized value. The version that returns a default value if the key doesn't exist might look something like this:
template <class Map> typename Map::data_type const & map_get(Map const & m, typename Map::key_type key) { static typename Map::data_type default_value; typename Map::const_iterator it = m.find(key); return (it == m.end() ? default_value : it->second); }