C++ 对象排序向量

C++ 对象排序向量,c++,sorting,vector,C++,Sorting,Vector,我有一个问题。这是我的密码。我有类卡和类卡。我的问题是函数直和sortFunc。我想对我在main中创建的名为fiveHand的向量进行排序。我看到了很多例子,但就我而言,我不明白如何正确地做到这一点 我有未声明的错误,如I和j: #include <iostream> using namespace std; class Card { private: int m_suit; int m_face; public:

我有一个问题。这是我的密码。我有
类卡
类卡
。我的问题是函数
sortFunc
。我想对我在
main
中创建的名为
fiveHand
的向量进行排序。我看到了很多例子,但就我而言,我不明白如何正确地做到这一点

我有未声明的错误,如
I
j

#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;
}
#包括
使用名称空间std;
班级卡
{
私人:
国际服装;
int m_脸;
公众:
卡片(内面、内套);
静态字符串适合[];
静态字符串面[];
静态串到串(串s_面、串s_套装);
int getFace();
无效设置面(内部面);
int getSuit();
无效诉讼(内部诉讼);
};
卡片::卡片(整型面,整型套装)
{
m_面=面;
m_suit=西装;
} 
字符串卡::脸[]={“A”,“平手”,“三”,“四”,“五”,
“六”、“七”、“八”、“九”、“十”、“杰克”、“女王”、“国王”};
串牌:套装[]={“红心”、“钻石”、“梅花”、“黑桃”};
int Card::getFace(){return m_face;}
无效卡片::setFace(int face){m_face=face;}
int Card::getSuit(){return m_suit;}
无效卡片::设置套装(内部套装){m_套装=套装;}
字符串卡::toString(字符串s_面,字符串s_套装)
{
串卡=s_面+“\tof\t”+s_套装;
回程卡;
}
#包括//cout
#包括//random\u shuffle
#包含//向量
#包括//时间
#包括
#包括“Card.h”
使用名称空间std;
甲板类
{
私人:
向量甲板;
公众:
纸牌;
静态整数计数;
静态int-next;
vector&getDeck(){return deck;}
虚空洗牌(向量和牌组);
Card dealCard();
bool-moreCards();
空对(矢量和五手);
第三类无效(矢量和五手);
四类空(矢量和五手);
无效冲洗(矢量和五手);
空直(矢量和五手);
bool-sortFunc(vector&fiveHand[i],vector&fiveHand[j]);
};
int deckOfCards::count=0;
int DECKOFCARD::next=0;
deckOfCards::deckOfCards()
{
const int FACES=13;
常数=4;
对于(int suit=0;suitsortFunc()
被声明为错误,您有:

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

如果要对
std::sort
vector
s进行排序,最好重载
问题的一部分似乎是
sortFunc
接受两个向量,并且尝试使用它对单个向量进行排序。它的定义如下:

bool sortFunc(const Card &first, const Card &second)
{
    if (first.getFace() < second.getFace())
        return true;
    else
        return false;
}
sort(fiveHand.begin(), fiveHand.end(), sortFunc);

另外,请注意,我将
sortFunc
定义为一个独立函数,而不是一个成员函数。如果您尝试使用
sortFunc
作为成员函数调用我上面显示的调用,它将不起作用,因为如果没有该类型的对象,您无法调用成员函数,
sort
没有
deckOfCards
o它可以使用的对象。

要添加到Ben的答案中,除了他建议的更改之外,您还需要进行
Card::getFace
以及可能的
Card::getSuit
const方法。否则,您不能在
const-Card
的实例上调用这些方法,就像
sortFunc
那样

int Card::getFace() const { return m_face; }
int Card::getSuit() const { return m_suit; }
注意:您可以选择使
sortFunc
成为本的想法那样的自由函数,也可以在
deckOfCards中使其成为静态函数:

class deckOfCards
{
public:
    // ...
    static bool sortFunc(const Card &lhs, const Card &rhs);
};
sortFunc
用于
类deckOfCards
的代码中,这可能会更好地进行通信

最后,您应该修改
sortFunc
,以便它进行稳定的比较:

bool deckOfCards::sortFunc(const Card &lhs, const Card &rhs)
{
  return !(rhs.getFace() < lhs.getFace());
}

你应该做一个等价的解释,并给出完整的错误解释,同时清楚地指出错误发生的地方。如果我按照你写代码的方式去做,代码会给我相同的错误,但不是I和j,而是第一个和第二个。@初学者,那是因为你用
第一个.getFace()替换
(fiveHand[I].getFace()
说得好,我忘了提到需要使那些成员函数
const
class deckOfCards
{
public:
    // ...
    static bool sortFunc(const Card &lhs, const Card &rhs);
};
bool deckOfCards::sortFunc(const Card &lhs, const Card &rhs)
{
  return !(rhs.getFace() < lhs.getFace());
}
std::sort(fiveHand.begin(), fiveHand.end(), deckOfCards::sortFunc);