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 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <iomanip>
template < typename T, typename U >
void push_back_all( std::vector<T>& vec, std::initializer_list<U> values )
{ vec.insert( vec.end(), values.begin(), values.end() ) ; }
int main()
{
std::map< std::string, std::vector<std::string> > map
{
{ "PLUTO", { "A DOG", "CUTE DOG", "THIS DOG BELONGS TO MICKEY" } },
{ "Neptune", { "Sylvester", "Bugs", "Daffy", "Tweety", "Wile E Coyote" } }
};
push_back_all( map["PLUTO"], { "Mickey", "Donald", "Huey" } ) ;
push_back_all( map["Jupiter"], { "d'Artagnan", "Athos", "Porthos", "Aramis" } ) ;
for( const std::string key : { "PLUTO", "Neptune", "Jupiter", "asdfgh" } )
{
std::cout << "key: " << std::quoted(key) ;
const auto iter = map.find(key) ;
if( iter == map.end() ) std::cout << " is not present in the map\n" ;
else
{
std::cout << " values (" << iter->second.size() << "): [ " ;
for( const auto& value : iter->second ) std::cout << std::quoted(value) << ' ' ;
std::cout << "]\n\n" ;
}
}
}
|