检查 std::array 内的对象是否具有相同的成员数据
To check objects inside std::array has identical member data
Cards.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;
}
hands.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();
hands.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 的成员数据是否有相同花色然后 return 同花..
我收到如下所示的错误
错误 3 error C2678: 二进制“==”: 未找到采用 'Card' 类型左操作数的运算符(或没有可接受的转换)
错误 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&)
作为谓词
P.S。上面的代码是在默认的 SO 文本编辑器中用心编写的,从未编译过。抱歉可能有错别字。
Cards.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;
}
hands.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();
hands.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 的成员数据是否有相同花色然后 return 同花..
我收到如下所示的错误
错误 3 error C2678: 二进制“==”: 未找到采用 'Card' 类型左操作数的运算符(或没有可接受的转换)
错误 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&)
作为谓词
P.S。上面的代码是在默认的 SO 文本编辑器中用心编写的,从未编译过。抱歉可能有错别字。