我需要编写一些代码来检查给定的整数是否在范围内。 (范围由一对整数表示。)
因此,给定一个定义为 std::pair
的范围 r
和一个测试整数 < code>n,我想说:
if (n >= r.first && n <= r.second)
问题是,我需要使用std::less
比较函子来执行此操作,这意味着我只能使用小于运算符。
我正在尝试想出等效的表达式。我很确定我的想法是正确的,但我并不完全有信心。
我想出的表达式是:
( !cmp(n, r.first) && !cmp(r.second, n) )
其中 cmp
是std::less
的实例
我这样做正确吗?
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:
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.
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) )
where cmp
is an instance of std::less<int>
Did I do that correctly?
发布评论
评论(3)
轮询其他人并不是验证正确性的最佳方法。 :)
相反,考虑你的问题。您正在处理的所有内容都是
int
,因此涉及的所有值都可以表示为int
。不涉及加法或减法,因此您不必担心超出可表示的范围。因此,我们可以使用标准整数回归标准数学,而将机器表示的混乱抛在脑后。您将获得一个两端封闭的范围
[n, m]
和一个值p
来测试该范围内的成员资格。您可以使用一个整数运算符<
。所有标准布尔运算符都是公平的游戏。现在,您可以简单地考虑集合。您想要拒绝所有
p
,使得p
p
p
或p
。 np >米。
p
的所有其他值都是可接受的。换句话说,如果使用 DeMorgan 定律,则
p
是所需集合的一部分,这相当于使用标准 C++ 运算符表示(而不是
提供的替代拼写) ;
),我们得到了您的建议,但使用了不同的名称:将
<()
重命名为cmp()
,n
到r.first
、m
到r.second
以及p
到n
,我们准确地得到了您的建议:所以,是的,对我来说看起来是正确的。
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 anint
. 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.You are given a range closed at both ends
[n, m]
and a valuep
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.Now, you can simply think about sets. You want to reject all
p
such thatp < n
orp > m
. All other values ofp
are acceptable. Put another way,p
is part of the desired set ifUsing DeMorgan's laws, this is equivalent to
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:Renaming
<()
tocmp()
,n
tor.first
,m
tor.second
, andp
ton
, we get precisely what you proposed:So, yup, looks correct to me.
是的,not of less-than 相当于大于或等于,事实上,在许多较旧的编程语言中
<=
实际上被称为ngt
表示不大于和 > ;= 是nlt
Yes, not of less-than is equivalent to greater than or equal to, in fact in many older programming languages
<=
is actually calledngt
for not greater than and >= isnlt
简短回答:
如果“num”在“min”和“max”之间但不等于其中任何一个,则返回 true。
如果您需要在范围检查中包含“min”和“max”,请使用:
这有效,因为......
Short answer:
This will return true if "num" is between "min" and "max" but is not equal to either of them.
If you need it to include "min" and "max" in the range check, use:
This works because...