[英]c++ bool While on vector
I'm new in c++, so this is maybe something usual. 我是C ++的新手,所以这可能是平常的事情。
I have a std::vector<int> v;
我有一个std::vector<int> v;
And I want to see if is 0 in this vector. 我想看看此向量中是否为0。 I think this is working: 我认为这是可行的:
bool l = true;
i = 0;
while((l = true) && (i<v.size()))
{
if (v[i] == 0) l = false;
++i;
}
But even if my vector is 1,2,3,0,2,0 this l
remains true
. 但是即使我的向量是1,2,3,0,2,0,这个l
仍然true
。 Why? 为什么? How can I resolve this? 我该如何解决?
while((l = true) && (i<v.size()))
is actually an assignment. 实际上是一项任务。 You should write more safe comparison code 您应该编写更安全的比较代码
while((true == l) && (i<v.size())
Notice that in this case if you accidentally type =
instead of ==
there will be a compilation error instead of broken code. 请注意,在这种情况下,如果您不小心键入=
而不是==
,则会出现编译错误而不是代码损坏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.