gpt4 book ai didi

c++ - 检查 std::array 中的对象是否具有相同的成员数据

转载 作者:行者123 更新时间:2023-11-28 02:25:11 24 4
gpt4 key购买 nike

卡片.h

class Card
{
public:

// Card suits
struct Suit
{
// Suits in order
enum Enum
{
Clubs,
Diamonds,
Hearts,
Spades,
};
};

// Card rank
struct Rank
{
// Ranks with aces low
enum Enum
{
Ace,
Two,
King,
....
...
};
};

// constructors
//get & set functions

//predicate

friend bool compareCardsSuit(const Card & cardlhs, const Card & cardrhs)
{
return cardlhs.GetSuit() == cardrhs.GetSuit();
}

friend bool operator==(Card const& lhs, Card const& rhs) // THis func is used for some other purpose
{
// We only care about rank during equality
return lhs.m_rank == rhs.m_rank;
}

手.h

class Hand
{
public:
static int const Size = 5;

// The type of hand we have
struct Type
{
// Type of hand in accending order
enum Enum
{
HighCard,// Five cards which do not form any of the combinations below
Flush, // Five cards of the same suit
bla,
bla..

};
};

// Object creation
// other functiosn



private:
mutable std::array<Card, Size> m_cards;
Type::Enum m_type;
// Hand calculations
Type::Enum Evaluate();

手.cpp

    Hand::Type::Enum Hand::Evaluate()
{

std::equal(m_cards.begin(), m_cards.end(), compareCardsSuit); // got error
{
return Hand::Type::Flush;
}
// Return our hand
return Hand::Type::HighCard;
}

我只想检查 m_cards 的成员数据是否具有相同花色然后返回同花..

我收到如下所示的错误

错误 3 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Card' (or there is no acceptable conversion)

错误 2 error C2171: '++' : 'bool (__cdecl *)(const Card &,const Card &)' 类型的操作数非法

最佳答案

要检查特定套装,您可以使用 std::all_of

const bool areAllClubs = std::all_of(m_cards.cbegin(), m_cards.cend(), 
[](const Card& card) {
return card.GetSuit() == Card::Suit::Clubs;
}));

要检查所有相邻的卡片是否符合某些标准,您可以使用 std::adjacent_find

const auto it = std::adjacent_find(m_cards.cbegin(), m_cards.cend(), 
[](const Card& left, const Card& right) {
return left.GetSuit() != right.GetSuit();
});
if (it == m_cards.end()) {
// All cards have same suit
}
else {
const auto& card = *it; // Points to a first card mismatched
}

或简单地

    const auto it = std::adjacent_find(m_cards.begin(), m_cards.end());

最后一个将使用operator==(const Card&, const Card&)作为谓词

附言上面的代码是在默认的 SO 文本编辑器中用心编写的,从未编译过。抱歉可能出现错别字。

关于c++ - 检查 std::array 中的对象是否具有相同的成员数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30968103/

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