Sort vector of pairs
Pergunta
I am making a poker evaluator, and for the histogram I am using vector< pair > to hold data. Now I am stuck on sorting them, whatever I try I get compiler error.
Any help is appreciated, thanks.
class Histogram {
private:
vector< pair<int,int> > _rankHistogram;
vector< pair<int,int> > _suitHistogram;
void add(int rank, int suit) {
for (int i=0; i < _rankHistogram.size(); i++)
if (_rankHistogram[i].first == rank) { _rankHistogram[i].second++; break; }
for (int i=0; i < _suitHistogram.size(); i++)
if (_suitHistogram[i].first == suit) { _suitHistogram[i].second++; break; }
}
bool cmp_big_first (pair<int,int> i, pair<int,int> j) { return i.second < j.second; }
public:
Histogram() {
_rankHistogram.push_back( make_pair(TWO, 0) );
_rankHistogram.push_back( make_pair(THREE, 0) );
_rankHistogram.push_back( make_pair(FOUR, 0) );
_rankHistogram.push_back( make_pair(FIVE, 0) );
_rankHistogram.push_back( make_pair(SIX, 0) );
_rankHistogram.push_back( make_pair(SEVEN, 0) );
_rankHistogram.push_back( make_pair(EIGHT, 0) );
_rankHistogram.push_back( make_pair(NINE, 0) );
_rankHistogram.push_back( make_pair(TEN, 0) );
_rankHistogram.push_back( make_pair(JACK, 0) );
_rankHistogram.push_back( make_pair(QUEEN, 0) );
_rankHistogram.push_back( make_pair(KING, 0) );
_rankHistogram.push_back( make_pair(ACE, 0) );
_suitHistogram.push_back( make_pair(SPADES, 0) );
_suitHistogram.push_back( make_pair(HEARTS, 0) );
_suitHistogram.push_back( make_pair(DIAMONDS, 0) );
_suitHistogram.push_back( make_pair(CLUBS, 0) );
}
void operator() (Card &c) { add(c.getRank(), c.getSuit()); }
void h_sort() {
sort(_rankHistogram.begin(), _rankHistogram.end(), cmp_big_first);
}
void h_print() {
for (int i=0; i<_rankHistogram.size(); i++) {
cout << Card::rankToString(_rankHistogram[i].first) << "\t\t" << _rankHistogram[i].second << endl;
}
}
Solução
You need to make it static cmp_big_first
. Without being static, the method takes an implicit this
parameter (of the class type), which does not make sense for your usage.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow