I am thinking about the problem of counting elements of specific values of map and how to achieve O(log(N)) complexity in C++. It is would be only possible when it is sorted by the values I guess, but then it would not be sorted by keys and the get from map would be then O(N).
In the first version I use std::count_if
. Here is my code:
std::count_if( std::begin(_map), std::end(_map),
[&val]( const std::pair<const std::string, std::string> &p )
{
return p.second == val;
} );
It has complexity the O(N). Do you know any other containers which would be useful for this problem? I was thinking about tree map but there might be a similar problem of sorting by keys.