仅使用 < 检查整数是否在范围内操作员

发布于 2024-09-28 04:50:18 字数 522 浏览 5 评论 0 原文

我需要编写一些代码来检查给定的整数是否在范围内。 (范围由一对整数表示。)

因此,给定一个定义为 std::pair 的范围 r 和一个测试整数 < code>n,我想说:

if (n >= r.first && n <= r.second)

问题是,我需要使用std::less 比较函子来执行此操作,这意味着我只能使用小于运算符。

我正在尝试想出等效的表达式。我很确定我的想法是正确的,但我并不完全有信心。

我想出的表达式是:

( !cmp(n, r.first) && !cmp(r.second, n) )

其中 cmpstd::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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤星 2024-10-05 04:50:18

轮询其他人并不是验证正确性的最佳方法。 :)

相反,考虑你的问题。您正在处理的所有内容都是 int,因此涉及的所有值都可以表示为 int。不涉及加法或减法,因此您不必担心超出可表示的范围。因此,我们可以使用标准整数回归标准数学,而将机器表示的混乱抛在脑后。

您将获得一个两端封闭的范围 [n, m] 和一个值 p 来测试该范围内的成员资格。您可以使用一个整数运算符 <。所有标准布尔运算符都是公平的游戏。

现在,您可以简单地考虑集合。您想要拒绝所有p,使得p p p p。 np >米。 p 的所有其他值都是可接受的。换句话说,如果

not ((p < n) or (m < p))

使用 DeMorgan 定律,则 p 是所需集合的一部分,这相当于

(not (p < n)) and (not (m < p))

使用标准 C++ 运算符表示(而不是 提供的替代拼写) ;),我们得到了您的建议,但使用了不同的名称:

!<(p, n) && !<(m, p)

<() 重命名为 cmp(), nr.firstmr.second 以及 pn ,我们准确地得到了您的建议:

!cmp(n, r.first) && !cmp(r.second, 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 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.

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.

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

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:

!<(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) && !cmp(r.second, n)

So, yup, looks correct to me.

面犯桃花 2024-10-05 04:50:18

是的,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 called ngt for not greater than and >= is nlt

柠北森屋 2024-10-05 04:50:18

简短回答:

if (num < max && !(num <= min)) { // stuff to do }

如果“num”在“min”和“max”之间但不等于其中任何一个,则返回 true。

如果您需要在范围检查中包含“min”和“max”,请使用:

if (num <= max && !(num < min)) { // stuff to do }

这有效,因为......

!(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

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.

If you need it to include "min" and "max" in the range check, use:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文