C++ 仅使用 < 运算符检查整数是否在范围内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3964017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if integer falls in range using only < operator
提问by Channel72
I need to come up with some code that checks if a given integer falls within the bounds of a range. (The range is represented by a pair of integers.)
我需要想出一些代码来检查给定的整数是否在一个范围内。(范围由一对整数表示。)
So, given a range r
defined as an std::pair<int, int>
, and a test integer n
, I want to say:
所以,给定一个r
定义为 an的范围std::pair<int, int>
和一个测试整数n
,我想说:
if (n >= r.first && n <= r.second)
if (n >= r.first && n <= r.second)
The catch is, I need to use a std::less<int>
comparison functor to do this, which means I can only work with the less than operator.
问题是,我需要使用std::less<int>
比较函子来做到这一点,这意味着我只能使用小于运算符。
I'm trying to come up with the equivalent expression. I'm pretty sure I have it correct, but I'm not entirely confident.
我正在尝试提出等效的表达式。我很确定我的说法是正确的,但我并不完全有信心。
The expression I came up with is:
我想出的表达是:
( !cmp(n, r.first) && !cmp(r.second, n) )
( !cmp(n, r.first) && !cmp(r.second, n) )
where cmp
is an instance of std::less<int>
哪里cmp
是一个实例std::less<int>
Did I do that correctly?
我做对了吗?
回答by Jeremy W. Sherman
Polling others is not the best way to verify correctness. :)
轮询他人并不是验证正确性的最佳方式。:)
Instead, consider your problem. Everything you are dealing with is an int
, so all values involved can be represented as an int
. No addition or subtraction is involved, so you needn't worry about leaving the representable range. So, we can fall back to standard math using standard integers, and leave the messiness of machine representations behind.
相反,请考虑您的问题。您处理的所有内容都是int
,因此所有涉及的值都可以表示为int
。不涉及加法或减法,因此您不必担心离开可表示范围。因此,我们可以使用标准整数回退到标准数学,而将机器表示的混乱抛诸脑后。
You are given a range closed at both ends [n, m]
and a value p
to test for membership in that range. You have one operator on integers that you can use, <
. All the standard boolean operators are fair game.
您将获得一个两端封闭的范围[n, m]
和一个值p
来测试该范围内的成员资格。您有一个可以使用的整数运算符,<
。所有标准的布尔运算符都是公平的游戏。
Now, you can simply think about sets. You want to reject all p
such that p < n
or p > m
. All other values of p
are acceptable. Put another way, p
is part of the desired set if
现在,您可以简单地考虑集合。你想拒绝所有p
这样的p < n
或p > m
。的所有其他值p
都可以接受。换句话说,p
是所需集合的一部分,如果
not ((p < n) or (m < p))
Using DeMorgan's laws, this is equivalent to
使用德摩根定律,这等价于
(not (p < n)) and (not (m < p))
Representing that using the standard C++ operators (rather than the alternative spellings provided by <iso646.h>
), we get what you proposed, but using different names:
表示使用标准 C++ 运算符(而不是 提供的替代拼写<iso646.h>
),我们得到您提出的建议,但使用不同的名称:
!<(p, n) && !<(m, p)
Renaming <()
to cmp()
, n
to r.first
, m
to r.second
, and p
to n
, we get precisely what you proposed:
重命名<()
到cmp()
,n
要r.first
,m
要r.second
,并p
到n
,我们给你提出的正是:
!cmp(n, r.first) && !cmp(r.second, n)
So, yup, looks correct to me.
所以,是的,在我看来是正确的。
回答by tobyodavies
Yes, not of less-than is equivalent to greater than or equal to, in fact in many older programming languages <=
is actually called ngt
for not greater than and >= is nlt
是的,不小于等于大于等于,实际上在很多老式编程语言<=
中实际上是叫ngt
不大于和 >= 是nlt
回答by Mr. Llama
Short answer:
简短的回答:
if (num < max && !(num <= min)) { // stuff to do }
This will return true if "num" is between "min" and "max" but is not equal to either of them.
如果“num”介于“min”和“max”之间但不等于其中任何一个,则返回true。
If you need it to include "min" and "max" in the range check, use:
如果您需要在范围检查中包含“min”和“max”,请使用:
if (num <= max && !(num < min)) { // stuff to do }
This works because...
这是有效的,因为...
!(A > B) == (A <= B) // If not greater than B, must be less than or equal to B
!(A >= B) == (A < B) // If not greater or equal to B, must be less than B
!(A < B) == (A >= B) // If not less than B, must be greater or equal to B
!(A <= B) == (A > B) // If not less than or equal to B, must be greater than B