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
|
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
struct card
{
int suitStrength;
int cardNum;
// ...
};
bool operator< ( const card& a, const card& b )
{ return std::tie(a.suitStrength,a.cardNum) < std::tie(b.suitStrength,b.cardNum) ; }
// return the lowest card in the hand
const card& lowest_card( std::vector<card>* hand )
{
if( !hand || hand->empty() ) throw std::invalid_argument( "bad arg hand" ) ;
return *std::min_element( hand->begin(), hand->end() ) ;
}
struct low_card_struct
{
// first == low card of the hand second == pointer to hand
using low_card_pair = std::pair< card, std::vector<card>* > ;
std::vector<low_card_pair> low_card_and_hand ;
};
// return low_card_struct with the member low_card_and_hand ordered on the low cards in each hand
low_card_struct order_on_lowest_card( std::vector< std::vector<card>* > hands )
{
low_card_struct lcs ;
for( auto ptr : hands ) lcs.low_card_and_hand.emplace_back( lowest_card(ptr), ptr ) ;
// sort on the lowest card in each hand
// note: the the comparison of pairs is lexicographic
// ie. the member first (the lowest card in the hand) is more significant
// in determining the order, which is what we want
std::sort( std::begin(lcs.low_card_and_hand), std::end(lcs.low_card_and_hand) ) ;
return lcs ;
}
int main()
{
std::vector<card> one(13), two(13), three(13), four(13) ;
// ... place cards into hands one, two, three, four
using std::addressof ;
low_card_struct lcs = order_on_lowest_card( { addressof(one), addressof(two), addressof(three), addressof(four) } ) ;
// lcs.low_card_and_hand[0].first: lowest card in the four hands,
// lcs.low_card_and_hand[0].second pointer to hand holding that lowest card
// lcs.low_card_and_hand[1].first: lowest card in the remaining three hands,
// lcs.low_card_and_hand[1].second pointer to hand holding that lowest card
// etc.
}
|