用于list :: sort的C++自定义比较函数

eli*_*ocs 3 c++ function-pointers list

嗨,我在编译一段简单的代码时遇到了麻烦.我正在创建一个实现卡片组的类,我想使用list :: short方法创建一个shuffle方法.

相关代码:

加入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 */
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

编译器显示下一条消息(忽略行号):

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*>]
Run Code Online (Sandbox Code Playgroud)

问题必须是我没有正确使用的compareRandom参考,我找不到谷歌搜索这个问题的答案.

提前致谢.

Ara*_*raK 10

我能说些什么 :)

首先,不要存储指针Card,只需将卡直接存储在容器中.如果您因任何原因坚持存储指针,请使用shared_ptr<Card>from Boost.其次,您可以使用std::random_shuffle并传递random-number-generator给它,而不是实现您的随机播放功能.


我可以再说一遍:)

这是我的想法,除非你list因为某种原因必须使用,尽管我没有see那个理由.

#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());
}
Run Code Online (Sandbox Code Playgroud)

我喜欢直接处理容器C++,但你可能不喜欢它.此外,如果您发现std::vector在您的情况下存在性能问题,则可以将typedef替换为std::deque:

typedef std::deque<Card> Deck;
Run Code Online (Sandbox Code Playgroud)


Log*_*ldo 7

compareRandom是一个成员函数,它有类型bool (Deck::*)(const Card*, const Card*),这意味着你不能调用它f(a,b),这就是sort将调用它的方式.您可以将compareRandom设置为静态或独立功能,或使用仿函数使其适应特定的Deck实例.


ags*_*mek 6

顺便说一句 - 你不能使用sort排序:) Sort对比较函数做了一些假设.