Hi there! I'm confused on how to sort boy and girl names. Like, in a country where I live, all of the boy names end with a 'as' or 'us' characters, and girl names end with a 'a' or 'e' characters. Here's a code where I find 10 most popular names in school. SO how to sort those 10 names?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for ( string name; in >> name; ) M[name]++;
for ( int i = 1; i <= 10 && !M.empty(); i++ )
{
auto it = max_element( M.begin(), M.end(), []( PR a, PR b )
{
return a.second < b.second || ( a.second == b.second && a.first > b.first );
} );
cout << i << ". " << (*it).first << ' ' << (*it).second << '\n';
fv << " " << i << ". " << (*it).first << endl;
M.erase(it);
}
you need to write your own comparison function for std::sort.
I would just check whether the last char is 's' for simplicity.
if you want them in order on top of being sorted boy/girl, you need to make it more complex, but only a little; if the input is boy/girl or girl/boy you sort off the last letter else sort normally with string compare...