How can I check whether multiple variables are equal to the same value?

后端 未结 3 1849
你的背包
你的背包 2020-11-30 15:11

How do I compare multiple items? For example, I wish to check if all the variables A, B, and C are equal to the char \'X\' or all three are equal to \'O\'. (If 2 of them are

相关标签:
3条回答
  • 2020-11-30 15:25
    if((A == 'X' || A == 'O') && A == B && B == C)
    {
        // Do whatever
    }
    
    0 讨论(0)
  • 2020-11-30 15:26

    Just for variety:

    template <typename T, typename U>
    bool allequal(const T &t, const U &u) {
        return t == u;
    }
    
    template <typename T, typename U, typename... Others>
    bool allequal(const T &t, const U &u, Others const &... args) {
        return (t == u) && allequal(u, args...);
    }
    
    if (allequal(a,b,c,'X') || allequal(a,b,c,'O')) { ... }
    
    0 讨论(0)
  • Just seperate them and test them one by one:

    if (A == 'O' && B == 'O' && C == 'O' || A == 'X' && B == 'X' && C == 'X')
        // etc
    
    0 讨论(0)
提交回复
热议问题