简体   繁体   English

对于c ++,有没有相当于Python的'in'关键字?

[英]Is there an equivalent to Python's 'in' keyword for c++?

I'm trying to write my c++ code more clearly and compactly, so let's say I'm trying to avoid statements like: 我正在尝试更清晰,更紧凑地编写我的c ++代码,所以让我说我试图避免这样的陈述:

if(MIN_VALUE <= a && a <= MAX_VALUE)

and replace it with something similar to: 并用类似的东西替换它:

if(a in [MIN_VALUE, MAX_VALUE])

You can use std::clamp which, unlike most STL functions, returns values in the closed interval [lo, hi] : 您可以使用std::clamp ,它与大多数STL函数不同,在闭区间[lo, hi]返回值:

if (v == std::clamp(v, lo, hi)) {
    // lo <= v && v <= hi.
}

No there isn't but you could build one yourself: 不,没有,但你可以自己建立一个:

template <typename T>
bool in(const T& a, const T& low, const T& high) 
{ 
    return a - low <= high - low; 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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