gpt4 book ai didi

c++ - 对象的排序 vector

转载 作者:行者123 更新时间:2023-11-30 04:12:06 25 4
gpt4 key购买 nike

我遇到了一个问题。这是我的代码。我有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com