C ++ list :: sort的自定义比较函数 [英] C++ Custom compare function for list::sort
问题描述
您好我无法编译一段简单的代码。我创建一个类实现一个卡片,我想使用list :: short方法创建一个shuffle方法。
Hi I'm having trouble compiling a simple piece of code. I am creating a class which implements a Deck of cards, and I want to create a shuffle method using the list::short method.
相关代码:
deck.h
#ifndef _DECK_H
#define _DECK_H
#include <list>
#include <ostream>
#include "Card.h"
#include "RandomGenerator.h"
using namespace std;
class Deck {
private:
static const int CARD_NUMBER = Card::CARDS_PER_SUIT*Card::SUIT_NUMBER;
list<Card *> *cards;
RandomGenerator rg;
public:
Deck();
~Deck();
void shuffle();
private:
bool const compareRandom(const Card *a, const Card *b);
};
#endif /* _DECK_H */
deck.cc: p>
deck.cc:
#include "Deck.h"
/**
* Fills the deck with a set of 52 cards
*/
Deck::Deck() {
cards = new list<Card *>();
for(int i = 0; i < CARD_NUMBER; i++)
cards->push_back(
new Card(
Card::Suit(int(i/Card::CARDS_PER_SUIT)),
i%Card::CARDS_PER_SUIT)
);
}
Deck::~Deck() {
gather();
for(list<Card *>::iterator c = cards->begin(); c != cards->end(); c++)
delete *c;
delete cards;
}
bool const Deck::compareRandom(const Card *a, const Card *b) {
return rg.randomBool();
}
void Deck::shuffle() {
cards->sort(compareRandom);
}
$ b <
The compiler shows the next message (ignore line numbers):
Deck.cc: In member function ‘void Deck::shuffle()’:
Deck.cc:66: error: no matching function for call to ‘std::list<Card*, std::allocator<Card*> >::sort(<unresolved overloaded function type>)’
/usr/include/c++/4.3/bits/list.tcc:303: note: candidates are: void std::list<_Tp, _Alloc>::sort() [with _Tp = Card*, _Alloc = std::allocator<Card*>]
/usr/include/c++/4.3/bits/list.tcc:380: note: void std::list<_Tp, _Alloc>::sort(_StrictWeakOrdering) [with _StrictWeakOrdering = const bool (Deck::*)(const Card*, const Card*), _Tp = Card*, _Alloc = std::allocator<Card*>]
问题必须在compareRandom引用上,无法找到搜索此问题的答案。
The problem has to be on the compareRandom reference that I'm not using correctly, I cant find googling the answer to this problem.
提前感谢。
推荐答案
我可以说什么:)
首先,不要存储指向 Card
卡直接在容器中。如果您坚持存储任何原因的指针,请使用 Boost
中的 shared_ptr< Card>
。第二,你可以使用 std :: random_shuffle
,并将随机数生成器
传递给它,而不是实现你的shuffle功能。
First, don't store a pointer to Card
, just store the cards directly in the container. If you insist on storing pointers to them for any reason, use shared_ptr<Card>
from Boost
. Second, you can use std::random_shuffle
and pass your random-number-generator
to it instead of implementing your your shuffle function.
我可以再说一遍:)
这是我的想法,除非你不得不使用 list
因为任何原因,虽然我不请参阅
原因。
This is what I have in mind, unless you have to use list
for whatever reason, although I don't see
that reason.
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
class Card
{
// ...
};
int main()
{
typedef std::vector<Card> Deck;
Deck deck;
// ... fill deck with cards.
// There is an optional third parameter,
// if you need to pass YOUR random-number-generator!
// If you do, I recommend Boost implementation.
std::random_shuffle(deck.begin(), deck.end());
}
我喜欢直接在 / code>,虽然你可能不喜欢它。另外,如果你看到
std :: vector
在你的情况下有性能问题,你可以用 std :: deque
:
I like to deal with containers directly in C++
, though you may not like it. Also, if you see that std::vector
has performance issues in your case, you could just replace the typedef with std::deque
:
typedef std::deque<Card> Deck;
这篇关于C ++ list :: sort的自定义比较函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!