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
|
#include <iostream>
#include <utility>
#include <algorithm>
#include <iterator>
enum colour_t { RED = 1, GREEN = 2, BLUE = 4 };
inline std::pair< colour_t, int > translate( std::pair< int, char > icpair )
{
if( icpair.second == 'G' || icpair.second == 'g' ) return { GREEN, icpair.first } ;
else if( icpair.second == 'B' || icpair.second == 'b' ) return { BLUE, icpair.first } ;
else return { RED, icpair.first } ;
}
int main()
{
std::pair< int, char > rgb[] { {5,'B'}, {8,'R'}, {1,'G'}, {2,'B'}, {2,'G'}, {7,'R'},
{5,'R'}, {8,'B'}, {8,'G'}, {6,'B'}, {7,'G'}, {9,'R'} } ;
std::sort( std::begin(rgb), std::end(rgb),
[]( const auto& icpa, const auto& icpb ) { return translate(icpa) < translate(icpb) ; } ) ;
for( const auto& icp : rgb ) std::cout << icp.first << ' ' << icp.second << '\n' ;
}
|