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

To check objects inside std::array has identical member data

本文关键字:成员 数据 对象 std array 检查 是否      更新时间:2023-10-16

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;
            }

手.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 错误 C2678:二进制"==":找不到采用类型为"Card"的左侧操作数的运算符(或者没有可接受的转换)

错误

2 错误 C2171:"++":在类型为"布尔 (__cdecl *)(常量卡 &,常量卡 &)"的操作数上非法

要检查特定的西装,您可以使用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 文本编辑器中用心编写的,从未编译过。很抱歉可能有错别字。