[英]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:我想知道在 c++11 中是否可以计算:
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) {}
) (我知道上面的代码行不通【它会计算是b还是c,然后检查结果是否等于a】。我想知道之前是否有类似的方法来实现代码:
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: output 将是:
it's worked for two equal string
it's worked for two non-equal string
it's worked for two equal int
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.