有没有等效于Python的c++"in"关键字?
Is there an equivalent to Python's 'in' keyword for c++?
我试图更清晰、更紧凑地编写我的 c++ 代码,所以假设我试图避免这样的语句:
if(MIN_VALUE <= a && a <= MAX_VALUE)
并将其替换为类似于以下内容的内容:
if(a in [MIN_VALUE, MAX_VALUE])
您可以使用std::clamp
,与大多数 STL 函数不同,它返回闭合区间[lo, hi]
的值:
if (v == std::clamp(v, lo, hi)) {
// lo <= v && v <= hi.
}
不,没有,但你可以自己构建一个:
template <typename T>
bool in(const T& a, const T& low, const T& high)
{
return a - low <= high - low;
}
相关文章:
- Visual Studio 2015:Extern "C" 和 "export" 关键字
- netcat command in c++
- C++中的"inline"关键字
- Difference in displaying cv2 Mat
- 如何确保C++函数在定义之前声明(如override关键字)
- C++ MFC Libraries in Travis CI
- 如何在OpenSSL中从configuration.h.in获取configuration.h
- 谷歌模拟和覆盖关键字
- 创建具有 new in 函数和"this is nullptr"异常的对象
- IN, OUT, INOUT Parameters
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 有人安装"IITB Simplecpp in mac"吗?
- 从 C 样式字符串中删除子字符串 "in place" 在C++代码中
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- 如果全局变量默认是外部变量,为什么要添加"extern"关键字?
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 如何将 'in' 关键字从 python 翻译成 c++?
- 有没有等效于Python的c++"in"关键字?
- 使用"auto"关键字时"redefinition"错误更改为"differs in levels of indirection"
- C# 中的 DLL C++等效(关键字 "IN")