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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
using multi_map = std::multimap< std::string, std::vector<std::string> > ;
bool contains( const std::vector<std::string>& values, const std::string& attribute )
{ return std::find( std::begin(values), std::end(values), attribute ) != std::end(values) ; }
std::vector<std::string> query( const multi_map& mmap, const std::string& key, const std::string& attribute )
{
std::vector<std::string> result ;
const auto pair = mmap.equal_range(key) ;
if( pair.first != mmap.end() )
{
for( auto iter = pair.first ; iter != pair.second ; ++iter )
{
if( contains( iter->second, attribute ) )
{
for( const std::string& str : iter->second )
if( !contains( result, str ) ) result.push_back(str) ;
}
}
}
return result ;
}
void print_query_results( const multi_map& mmap, const std::string& key, const std::string& attribute )
{
std::cout << std::quoted(key) << " + " << std::quoted(attribute) << " => [" ;
for( const auto& str : query( mmap, key, attribute ) ) std::cout << std::quoted(str) << ' ' ;
std::cout << "]\n" ;
}
int main()
{
multi_map mmap =
{
{ "pluto", { "dog", "10", "male" } },
{ "pluto", { "planet", "2", "solar system" } },
{ "pluto", { "dwarf", "planet", "134340", "solar system" } },
{ "apple", { "fruit", "delicious" } },
{ "apple", { "business", "inc.", "cupertino", "california" } },
{ "apple", { "computer", "macbook", "OS X", "BSD" } },
{ "apple", { "tree", "deciduous", "rosa" } },
};
print_query_results( mmap, "pluto", "dog" ) ;
print_query_results( mmap, "apple", "BSD" ) ;
print_query_results( mmap, "pluto", "solar system" ) ;
}
|