[英]Is there a shorter way to calculate if (a == b || a == c) in C++
我想知道在 c++11 中是否可以計算:
if (a == b || a == c) {
// do something
}
以更短更簡潔的方式,例如:
if (a == (b || c)) {
// do something
}
(我知道上面的代碼行不通【它會計算是b還是c,然后檢查結果是否等於a】。我想知道之前是否有類似的方法來實現代碼: if (a == b || a == c) {}
)
考慮:
#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";
}
}
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.