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
|
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
struct A
{
int value() const { return v ; }
const std::string& str() const { return s ; }
int v ;
std::string s ;
};
void foo( std::vector<A>& first, std::vector<A>& second )
{
std::vector<A> third ;
// merge first and second into third ordered on v
auto cmp = [] ( const A& a, const A& b ) { return a.value() < b.value() ; } ;
std::sort( first.begin(), first.end(), cmp ) ; // sort first on ascending v
std::sort( second.begin(), second.end(), cmp ) ; // sort second on ascending v
std::merge( first.begin(), first.end(), second.begin(), second.end(),
std::back_inserter(third), cmp ) ; // merge first and second into third
// append contents of second to first
first.insert( first.end(), second.begin(), second.end() ) ;
// sort first on descending length of str
std::sort( first.begin(), first.end(),
[] ( const A& a, const A& b ) { return a.str().size() > b.str().size() ; } ) ;
}
|