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
|
#include <iostream>
#include <vector>
#include <string>
#include <map>
using vec2d = std::vector< std::vector<std::string> > ;
using row_map = std::map< std::pair<std::string,std::string>, std::vector<std::string> > ;
std::vector<std::string>& combine( std::vector<std::string>& a, std::vector<std::string> b )
{
std::size_t sz = std::max( a.size(), b.size() ) ;
a.resize(sz) ;
b.resize(sz) ;
for( std::size_t i = 0 ; i < sz ; ++i ) a[i] = std::max( a[i], b[i] ) ;
return a ;
}
row_map make_row_map( const vec2d& vec )
{
row_map result ;
for( const auto& v : vec )
if( v.size() > 2 ) combine( result[ { v[0], v[1] } ], v ) ;
return result ;
}
vec2d combine_rows( const vec2d& vec )
{
const auto row_map = make_row_map(vec) ;
vec2d result ;
for( auto& pair : row_map ) result.push_back( std::move(pair.second) ) ;
return result ;
}
int main() // trivial test driver
{
const vec2d original =
{
{ "Stroustrup", "Bjarne", "4" },
{ "Koenig", "Andrew", "0", "0", "9" },
{ "Stroustrup", "Bjarne", "0", "0", "0", "8" },
{ "Koenig", "Andrew", "6" },
{ "Stroustrup", "Bjarne", "0", "2" },
{ "Koenig", "Andrew", "0", "1", "0", "5" },
{ "Stroustrup", "Bjarne", "0", "0", "6" },
};
for( const auto& row : combine_rows(original) )
{
for( const auto& str : row ) std::cout << str << ' ' ;
std::cout << '\n' ;
}
}
|