1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <iostream>
#include <map>
#include <iomanip>
template < typename KEY_TYPE, typename MAPPED_TYPE, typename KEYCMP, typename ALLOCATOR >
std::size_t count_mapped_values( const std::map<KEY_TYPE,MAPPED_TYPE,KEYCMP,ALLOCATOR>& map, const MAPPED_TYPE& value )
{
std::size_t cnt = 0 ;
for( const auto& pair : map ) if( value == pair.second ) ++cnt ;
return cnt ;
}
int main()
{
std::map< std::string, std::string > map { { "one", "1" }, { "two", "2" }, { "three", "3" },
{ "un", "1" }, { "seux", "2" },
{ "eins", "1" }, { "zwei", "2" },
{ "dos", "2" } } ;
for( int i = 1 ; i < 5 ; ++i )
{
std::cout << "count of value " << std::quoted( std::to_string(i) ) << " : "
<< count_mapped_values( map, std::to_string(i) ) << '\n' ;
}
}
|