gpt4 book ai didi

c++ - 尝试将 std::sort() 与自定义对象一起使用

转载 作者:行者123 更新时间:2023-11-28 00:46:07 31 4
gpt4 key购买 nike

我在网上查了一些教程。我已经在我的身上试过了,但问题是 std::sort()不执行任何操作!这是我的全部代码:

#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;

#ifndef _card_h
#define _card_h

enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES
};

enum Rank {
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
};

class Card {
public:
Card();
Card(Rank, Suit);
~Card();
Rank GetRank();
Suit GetSuit();
string ToString();
private:
Rank rank;
Suit suit;
};
#endif

Card::Card() {
}

Card::Card(Rank rank, Suit suit) {
this->rank = rank;
this->suit = suit;
}

Card::~Card() {}

Rank Card::GetRank() {
return rank;
}

Suit Card::GetSuit() {
return suit;
}

string Card::ToString() {
string cardName = "";
switch (rank) {
case TWO : cardName += "2"; break;
case THREE : cardName += "3"; break;
case FOUR : cardName += "4"; break;
case FIVE : cardName += "5"; break;
case SIX : cardName += "6"; break;
case SEVEN : cardName += "7"; break;
case EIGHT : cardName += "8"; break;
case NINE : cardName += "9"; break;
case TEN : cardName += "T"; break;
case JACK : cardName += "J"; break;
case QUEEN : cardName += "Q"; break;
case KING : cardName += "K"; break;
case ACE : cardName += "A"; break;
}
switch (suit) {
case CLUBS : cardName += "C"; break;
case DIAMONDS : cardName += "D"; break;
case HEARTS : cardName += "H"; break;
case SPADES : cardName += "S"; break;
}
return cardName;
}

#ifndef _cardcomparer_h
#define _cardcomparer_h

class CardComparer {
public:
bool operator() (Card*, Card*);
private:
Card* firstCard;
Card* secondCard;
};
#endif

bool CardComparer::operator() (Card* firstCard, Card* secondCard) {
this->firstCard = firstCard;
this->secondCard = secondCard;
cout << "in Cardcompare! " << endl;
if (firstCard->GetRank() == secondCard->GetRank()) {
return firstCard->GetSuit() > secondCard->GetSuit();
}
else {
return firstCard->GetRank() > secondCard->GetRank();
}
}

#ifndef _hand_h
#define _hand_h
const int CARDS_IN_HAND = 5;
class Hand {
public:
Hand(int);
~Hand();
void AddCard(Card*);
string ToString();

private:
int cardCount;
int playerID;
vector<Card*> cards;

};
#endif

Hand::Hand(int playerID) {
this->playerID = playerID;
cards.reserve(CARDS_IN_HAND);
cardCount = 0;
}

Hand::~Hand() {
cards.clear();
}

void Hand::AddCard(Card* newCard) {
cards[cardCount] = newCard;
cardCount++;
sort(cards.begin(), cards.end(),CardComparer());
}

string Hand::ToString() {
stringstream playerCards;
playerCards << "Player " << this->playerID << " -";
for (int i = 0; i < cardCount; i++ ){
playerCards << " " << cards[i]->ToString();
}
return playerCards.str();
}

int main() {
vector<Hand*> hands;
hands.reserve(1);
hands[0] = new Hand(0);
hands[0]->AddCard(new Card((Rank)4, (Suit)1));
hands[0]->AddCard(new Card((Rank)8, (Suit)2));
hands[0]->AddCard(new Card((Rank)5, (Suit)1));
hands[0]->AddCard(new Card((Rank)2, (Suit)0));
hands[0]->AddCard(new Card((Rank)7, (Suit)3));
cout << hands[0]->ToString() << endl;
return 0;
}

问题是第 133 行(sort(cards.begin(), cards.end(),CardComparer());)没有执行。无论我将其注释掉还是在结果中都没有改变,我什至在 93(cout << "in Cardcompare! " << endl;)上添加了额外的一行,如果它使用 bool,它会打印出一些东西。然而事实并非如此。我找不到问题出在哪里。这里有什么问题,我该如何解决?

最佳答案

问题是 vector 不知道您尝试在 AddCard 中添加卡片,或者更准确地说,您没有真的添加这些卡片,因为您确实不调用 push_back 而是简单地将指针放在内存中的某个位置。是的,由于调用 reserve,vector 拥有该内存,但它认为它是空的。因此 begin()end() 给出了相同的迭代器并且 sort 正好对 0 个元素进行排序。

在您选择的教科书中查找有关 vector 和其他容器的章节。您不需要跟踪卡片的数量 (cardCount),因为如果使用得当, vector 会为您完美地完成这项工作。

关于c++ - 尝试将 std::sort() 与自定义对象一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16184728/

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