【问题标题】:To check objects inside std::array has identical member data检查 std::array 内的对象是否具有相同的成员数据
【发布时间】:2015-06-21 18:48:48
【问题描述】:

卡片.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 的成员数据是否具有相同的花色,然后返回同花顺..

我收到如下所示的错误

错误 3 错误 C2678: 二进制 '==' : 未找到采用 'Card' 类型的左操作数的运算符(或没有可接受的转换)

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

【问题讨论】:

  • 恐怕您必须自己编写== 运算符。您的来源不包含++,所以我不确定如何处理该错误。
  • @Mr Lister。谢谢问题是我已经为等级编写了相等运算符。此运算符用于某些不同的目的。现在,我怎样才能再次为具有与等级相同参数的西装编写相同的运算符。编译器不允许我.. 还有什么错在我的代码?
  • 嗯?为什么编译器不允许您为不同的类编写 similatr 运算符?您可以在我们可以检查的问题中发布minimal, complete and verifiable example 吗?
  • 不要使用std::equal,使用std::find并与m_cards.end()比较,看看是否找到了元素(即if (find(...) != m_cards.end()) { ok })。
  • @ 0X499602D2 , by std::find 我如何确保数组的所有对象都具有相同的套装,例如所有对象都有俱乐部?

标签: c++ std equality


【解决方案1】:

要检查特定的西装,您可以使用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&amp;, const Card&amp;)作为谓词

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多