是否&&在 c++行为与 && 相同在Java中?
我的问题基本上就在标题中。基本上我已经了解到在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,在 C 和 C++ 中 &&和 ||操作员短路。
Yes, in C and C++ the && and || operators short-circuit.
快捷方式在 C、C++ 和 Java 中的工作方式相同。
但是,根据示例代码,如果
list
为null
,则可能会出现段错误。 C++ 没有相当于 Java 的 NullPointerException。如果列表可能为空,您也需要进行测试。已更新
后半部分仅适用于 list 是指针的情况。 (这似乎不是。)如果是这样的话,它看起来像:
Shortcutting works the same in C, C++, and Java.
However, given the example code, you may get a segfault if
list
isnull
. 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:
是的,
&&
在 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.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.
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 ifi < list.size()
is false.&&和 ||如果没有过载,操作员会短路。然而,我相信它们可以过载,在这种情况下,如果在过载有效的类型上使用它们,它们就不会短路。
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.