是否&&在 c++行为与 && 相同在Java中?

发布于 2024-09-26 08:33:35 字数 441 浏览 1 评论 0原文

我的问题基本上就在标题中。基本上我已经了解到在 Java 中 &&运算符的作用类似于短路,因此如果第一个条件的计算结果为 false,则它不会查看语句的其余部分。我假设 C++ 就是这种情况,但我正在编写一些代码,它首先检查索引是否超过列表大小,然后将列表中的索引与另一个数字进行比较。类似于:

//let's say list.size()=4;

for(int i=0; i<10; i++)
{
   if(i < list.size() && list.get(i) == 5)
       //do something
   ...
}

这不是确切的代码,但它说明了这一点。我假设因为 i >后半部分的列表大小不会被评估。但看起来它仍然如此,并且我相信这导致了段错误。我知道我可以使用嵌套的 if,但这太碍眼而且浪费空间。有什么帮助吗?

my question is essentially in the title. Basically I've learned that in Java the && operator acts like a short circuit, so that if the first condition evaluates to false it doesn't look at the rest of the statement. I assumed this was the case in c++ but I'm writing a bit of code which first checks that an index has not exceeded a list size, then compares that index in the list to another number. Something like:

//let's say list.size()=4;

for(int i=0; i<10; i++)
{
   if(i < list.size() && list.get(i) == 5)
       //do something
   ...
}

That's not the exact code but it illustrates the point. I assume that since i > the list size the second half won't get evaluated. But it appears that it still does, and I believe this is causing a Seg Fault. I know I can use nested ifs but that's such an eyesore and a waste of space. Any help?

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

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

发布评论

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

评论(6

初见 2024-10-03 08:33:35

是的,在 C 和 C++ 中 &&和 ||操作员短路。

Yes, in C and C++ the && and || operators short-circuit.

巴黎盛开的樱花 2024-10-03 08:33:35

快捷方式在 C、C++ 和 Java 中的工作方式相同。

但是,根据示例代码,如果 listnull,则可能会出现段错误。 C++ 没有相当于 Java 的 NullPointerException。如果列表可能为空,您也需要进行测试。

已更新
后半部分仅适用于 list 是指针的情况。 (这似乎不是。)如果是这样的话,它看起来像:

if (list && i < list->size() && list->get(i) == 5)

Shortcutting works the same in C, C++, and Java.

However, given the example code, you may get a segfault if list is null. C++ has no equivalent to Java's NullPointerException. If list might be null, you need to test for that as well.

Updated
The latter half of that only applies if list is a pointer. (Which is does not appear to be.) If that was the case it would look like:

if (list && i < list->size() && list->get(i) == 5)
甜柠檬 2024-10-03 08:33:35

是的,&& 在 Java 中的行为与在 C++ 中的行为类似。它是一个短路运算符,也是一个序列点[在 C++ 中]。操作数的求值顺序被明确定义,即从左到右。

Yes && behaves similarly in Java as in C++. It is a short circuiting operator and also a sequence point [in C++]. The order of evaluation of operands is well defined i.e from left to right.

魔法少女 2024-10-03 08:33:35

C++ &&和 ||运营商也会短路。小心,也许你放了一个 &而C++使用的是二元与运算符,这不是短路的。如果您需要更多帮助,请发布更多相关代码。

C++ && and || operators do shortcircuiting too. Be careful, maybe you put a single & and C++ is using tbe binary and operator, which is not shortcircuited. Post more relevant code if you need more help.

ㄖ落Θ余辉 2024-10-03 08:33:35

C++ 短路 &&和 ||就像Java一样。在 if(i < list.size() && list.get(i)) 中,如果 list.get(i) 将不会被计算>我< list.size() 为 false。

C++ short circuits && and || just like Java. in if(i < list.size() && list.get(i)) , list.get(i) will not be evaluated if i < list.size() is false.

梦屿孤独相伴 2024-10-03 08:33:35

&&和 ||如果没有过载,操作员会短路。然而,我相信它们可以过载,在这种情况下,如果在过载有效的类型上使用它们,它们就不会短路。

The && and || operators short-circuit if not overloaded. I believe they can be overloaded, however, in which case they would not short-circuit if used on types for which an overload was in effect.

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