A multimap
's size
reports the number of values it contains. I'm interested in the number of keys it contains. For example, given multimap<int, double> foo
I'd like to be able to do this:
const auto keyCount = ???
One way to get this is to use a for
-loop on a zero initialized keyCount
:
for(auto it = cbegin(foo); foo != cend(foo); it = foo.upper_bound(foo->first)) ++keyCount;
This however, does not let me perform the operation inline. So I can't initialize a const auto keyCount
.
A solution could be a lambda or function which wraps this for
-loop such as:
template <typename Key, typename Value>
size_t getKeyCount(const multimap<Key, Value>& arg) {
size_t result = 0U;
for(auto it = cbegin(foo); foo != cend(foo); it = foo.upper_bound(foo->first)) ++result;
return result;
}
But I was hoping for something provided by the standard. Does such a thing exist?