C中的最大值和最小值是多少#定义函数
What are the MAX and MIN in C? #define function
我是一名学生,我的老师给了我一个已经解决的学习练习,在他的练习中我看到了这行:
#define MIN(a,b) ((a) < (b) ? (a) : (b))
我以前从未使用过#define。
我不明白什么:
((a) < (b) ? (a) : (b))
代表。
看起来像是"?"是一个不确定的比较器。有人能帮我吗?
它被称为条件运算符(或三元运算符(
#define MIN(a,b) ((a) < (b) ? (a) : (b))
平均值:
if ((a) < (b)){
return a;
} else {
return b;
}
所以如果你愿意的话:
int test = MIN(5,10);
测试将是5
链接到wiki页面时出现问题:http://goo.gl/bw2sL
#define
定义了一个新的预处理器宏,在本例中,它将MIN代码放在您放置它的位置;a
和b
的"变量"将替换为您作为输入提供给宏的任何变量。
#define MIN(a,b) ((a) < (b) ? (a) : (b))
MIN(5,6);
//gets expanded to
((5) < (6) ? (5) : (6))
实际的表达式是使用三元运算符,根据表达式的求值结果返回A或B,您可以在这里阅读更多信息:
http://en.cppreference.com/w/cpp/language/operator_other
最后,当你用c++标记你的问题时,请考虑非宏的max和min函数。
#include <algorithm>
...
int i=std::min(5,6);
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在命名空间中定义函数还是限定函数
- 复制列表初始化的隐式转换的等级是多少
- 此代码是否违反一个定义规则
- 定义类型的成本/开销是多少?
- 如果将功能的非常简单的定义移动到.cpp,则编译时间的减少是多少
- 将 true 重新定义为 false 时的期望输出是多少,反之亦然
- C++中变量定义运算的值是多少
- std::variant可以定义多少类型
- 我应该在知道我将使用多少个元素之前还是之后定义向量中的元素数?
- C中的最大值和最小值是多少#定义函数
- 如何定义在 SFML 游戏中使用多少 CPU