简体   繁体   中英

Is there a shorter way to calculate if (a == b || a == c) in C++

I am wondering if in c++11 you can calculate this:

if (a == b || a == c) {
    // do something
}

In a much shorter and more concise way such as something like this:

if (a == (b || c)) {
    // do something
}

(I know that the above code would not work [it would calculate if b or c and then check if the result is equal to a]. I am wondering if there is a similar way to implement the code before: if (a == b || a == c) {} )

Consider:

#include <iostream>
#include <vector>

template <typename T>
bool operator==(const T t, std::vector<T> x) {
    for(const auto& v: x)
        if(t != v) {
            return false;
        }
    return true;
}

int main() {
    if ("s" == std::vector{"s", "s"}) {
        std::cout << "it's worked for two equal string\n";
    }

    if (!("s" == std::vector{"a", "s"})) {
        std::cout << "it's worked for two non-equal string\n";
    }

    if (1 == std::vector{1, 1}) {
        std::cout << "it's worked for two equal int\n";
    }
}

the output will be:

it's worked for two equal string
it's worked for two non-equal string
it's worked for two equal int

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM