c++ - 对象的排序 vector

标签 c++ sorting vector

我遇到了一个问题。这是我的代码。我有 class Cardclass deckOfCards。我的问题出在 straightsortFunc 函数中。我想对我在 main 中创建的名为 fiveHand 的 vector 进行排序。我看到了很多示例,但就我而言,我无法理解如何正确执行此操作。

我有类似ij 未声明的错误:

#include <iostream>

using namespace std;


class Card
{
    private:
        int m_suit;
        int m_face;
    public:
        Card(int face, int suit);
        static string suits[];
        static string faces[];
        static string toString(string s_face, string s_suit);
        int getFace();
        void setFace(int face);
        int getSuit();
        void setSuit(int suit);
};

Card::Card(int face, int suit)
{
    m_face = face;
    m_suit = suit;
} 
string Card::faces[] = {"Ace", "Deuce", "Three", "Four", "Five", 
"Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

string Card::suits[] = {"hearts", "diamonds", "clubs", "spades"};

int Card::getFace(){return m_face;}
void Card::setFace(int face){m_face = face;}
int Card::getSuit(){return m_suit;}
void Card::setSuit(int suit){m_suit = suit;}

string Card::toString(string s_face, string s_suit)
{
    string card = s_face + "\tof\t " + s_suit;
    return card;
}




#include <iostream>     // cout
#include <algorithm>    // random_shuffle
#include <vector>       // vector
#include <ctime>        // time
#include <cstdlib> 
#include "Card.h"

using namespace std;

class deckOfCards
{
    private:
        vector<Card> deck;

    public:
        deckOfCards();
        static int count;
        static int next;
        vector<Card>& getDeck() {return deck;}
        void shuffle(vector<Card>& deck);
        Card dealCard();
        bool moreCards();
        void pairs(vector<Card>& fiveHand);
        void threeOfKind(vector<Card>& fiveHand);
        void fourOfKind(vector<Card>& fiveHand);
        void flush(vector<Card>& fiveHand);
        void straight(vector<Card>& fiveHand);
        bool sortFunc(vector<Card>&  fiveHand[i], vector<Card>&  fiveHand[j]);

};

int deckOfCards::count = 0;
int deckOfCards::next = 0;

deckOfCards::deckOfCards()
{
    const int FACES = 13;
    const int SUITS = 4;
    for (int suit = 0; suit < SUITS; suit++)
        {
            for (int face = 0; face < FACES; face++)
                {
                    Card card = Card(face, suit); //card created and initialized
                    deck.push_back(card);
                }
        }

}

void deckOfCards::shuffle(vector<Card>& deck)
{
    srand(static_cast<unsigned int>(time(0)));  
    random_shuffle(deck.begin(), deck.end());
}

Card deckOfCards::dealCard()
{
    Card nextCard = deck[next];
    next++;
    return nextCard;
}

bool deckOfCards::moreCards()
{
    const int FIVE_CARD = 5;
    if (count < FIVE_CARD)
    {
        count++;
        return true;
    }
    else
        return false;
}

void deckOfCards::pairs(vector<Card>& fiveHand)
{
    int pair = 0;
    const int MAX_CARDS = 5;
    for (int i = 0; i < MAX_CARDS; i++)
    {
        int j = i+1;
        for (j; j < MAX_CARDS; j++)
        {
            if (fiveHand[i].getFace() == fiveHand[j].getFace())
                pair++;
        }
    }
    if (pair == 1)
    {
        cout << "You have one pair!" << endl;
    }
    else if (pair == 2)
    {
        cout << "You have two pairs!" << endl;
    }
}


void deckOfCards::threeOfKind(vector<Card>& fiveHand)
{
    if (((fiveHand[0].getFace() == fiveHand[1].getFace())&&(fiveHand[1].getFace() == fiveHand[2].getFace()))
        ||((fiveHand[1].getFace() == fiveHand[2].getFace())&&(fiveHand[2].getFace() == fiveHand[3].getFace()))
        ||((fiveHand[2].getFace() == fiveHand[3].getFace())&&(fiveHand[3].getFace() == fiveHand[4].getFace())))
        {
            cout << "Your hand contains three cards of the same faces!" << endl;
        }

}

void deckOfCards::fourOfKind(vector<Card>& fiveHand)
{
    if (((fiveHand[0].getFace() == fiveHand[1].getFace())
        &&(fiveHand[1].getFace()== fiveHand[2].getFace())
        &&(fiveHand[2].getFace()== fiveHand[3].getFace()))
        ||((fiveHand[1].getFace() == fiveHand[2].getFace())
        &&(fiveHand[2].getFace() == fiveHand[3].getFace())
        &&(fiveHand[3].getFace() == fiveHand[4].getFace())))
    {
        cout << "Your hand contains four cards of the same faces!" << endl;
    }   
}

void deckOfCards::flush(vector<Card>& fiveHand)
{
    if ((fiveHand[0].getSuit() == fiveHand[1].getSuit())
        &&(fiveHand[1].getSuit() == fiveHand[2].getSuit())
        &&(fiveHand[2].getSuit() == fiveHand[3].getSuit())
        &&(fiveHand[3].getSuit() == fiveHand[4].getSuit()))
        {
            cout << "Congradulations!!! You have a flush!!!" << endl;
        }   
}

void deckOfCards::straight(vector<Card>& fiveHand)
{
    //sort cards based on faces
    sort(fiveHand.begin(), fiveHand.end(), sortFunc(vector<Card>&  fiveHand[i], vector<Card>&  fiveHand[j])); 
    if ((fiveHand[1].getFace() == (fiveHand[0].getFace()+1))
        &&(fiveHand[2].getFace() == (fiveHand[1].getFace()+1))
        &&(fiveHand[3].getFace() == (fiveHand[2].getFace()+1))
        &&(fiveHand[4].getFace() == (fiveHand[3].getFace()+1)))
        {
            cout << "You're so lucky!!! You have a straight!" << endl;
        }
}

bool deckOfCards::sortFunc(vector<Card>&  fiveHand[i], vector<Card>&  fiveHand[j])
{
    if (fiveHand[i].getFace() < fiveHand[j].getFace())
        return true;
    else
        return false;
}



#include "deckOfCards.h"


using namespace std;

int main(int argc, char** argv) 
{
    deckOfCards cardDeck; // create DeckOfCards object
    vector<Card> fiveHand;
    cardDeck.shuffle(cardDeck.getDeck()); // shuffle the cards in the deck

    cout << "Your cards are: \n" << endl;
    while (cardDeck.moreCards() == true)
    {
        Card currCard = cardDeck.dealCard();
        fiveHand.push_back(currCard);
        string face = Card::faces[currCard.getFace()];
        string suit = Card::suits[currCard.getSuit()];
        string card = Card::toString(face,suit);
        cout << card << endl;
    }
    cout << endl;
    cardDeck.pairs(fiveHand);
    cardDeck.threeOfKind(fiveHand);
    cardDeck.fourOfKind(fiveHand);
    cardDeck.flush(fiveHand);
    cardDeck.straight(fiveHand);

    return 0;
}

最佳答案

sortFunc()被声明为错误,你有:

 bool deckOfCards::sortFunc(vector<Card>&  fiveHand[i], vector<Card>&  fiveHand[j])

在哪里ij未声明,正确的函数声明应该是:

 //if you want to call sortFunc like sortFunc(deck[0], deck[1])
 bool deckOfCards::sortFunc(Card& first, Card& second)
 //if you want to call sortFunc like sortFunc(deck0, deck1), then
 bool deckOfCards::sortFunc(vector<Card>& first, vector<Card>& second)

如果你想std::sort vector<Card> s,最好让 < 过载运算符(operator):

friend bool operator<(const Card &first, const Card &second); //to Card class
bool operator<(const Card &first, const Card &second)
{
    if (first.getFace() < second.getFace())
        return true;
    return false;
}

关于c++ - 对象的排序 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19943795/

相关文章:

java - 数组排序方法

C++ 将 Vector<BYTE> 转换为第一个 vector 字节为 0 的字符串

c++ - 从文本文件读取行到 std::vector<string> 的最有效方法

C++ For 循环遍历结构 vector (包含更多结构 vector )

c++ - 何时使用 "::"何时使用 "."

c++ - 一对 C++ 交换方法的优点是什么?

c++ - 按在 C++ 中接受参数的函数排序?

c++ - vs2010 (c++, win32) 文件写入

c++ - 将结构化数据投影到 std::vector 的最有效方法是什么?

vba - 使用 Excel VBA 按列值对工作表数据进行排序