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 58 59 60 61 62 63
|
#include <map>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
using map_type = std::multimap< std::string, std::vector<std::string> > ;
void insert_first( std::istream& stm, map_type& map )
{
std::string first, second ;
while( std::getline( stm >> std::ws, first, ',' ) &&
std::getline( stm >> std::ws, second ) ) map.emplace( first, std::vector<std::string>( 1, second ) ) ;
}
void insert_second( std::istream& stm, map_type& map )
{
std::string first, second ;
while( std::getline( stm >> std::ws, first, ',' ) &&
std::getline( stm >> std::ws, second ) )
{
auto bounds = map.equal_range(first) ;
if( bounds.first == bounds.second ) // not found, insert key and value
map.emplace( first, std::vector<std::string>( 1, second ) ) ;
else for( auto iter = bounds.first ; iter != bounds.second ; ++iter ) // found, append the value to all keys
iter->second.push_back(second) ;
}
}
int main()
{
std::istringstream file1
(
"red, apple\n"
"red, bird\n"
"green, truck\n"
"blue, car\n"
"yellow, ball\n"
"orange, candy\n"
) ;
std::istringstream file2
(
"gold, necklace\n"
"green, tree\n"
"yellow, sticker\n"
"blue, water\n"
"red, bag\n"
) ;
map_type map ;
insert_first( file1, map ) ;
insert_second( file2, map ) ;
for( const auto& pair : map )
{
std::cout << pair.first ;
for( const std::string& s : pair.second ) std::cout << ", " << s ;
std::cout << '\n' ;
}
}
|