std::out_of_range 错误
我正在 Linux Ubuntu 中的 opencv 中处理以下代码。 x_captured 和 y_captured 是“int”类型向量。两个向量的大小都是 18。
for (int i=0;i<=x_captured.size();i++)
{
for (int j=0;j<=y_captured.size();j++)
{
if (i!=j)
{
if (((x_captured.at(j)-x_captured.at(i))<=2) &&
((y_captured.at(j)-y_captured.at(i))<=2))
{
consecutive=consecutive+1;
}
}
}
}
但是当 i=0 且 j=18 之后,它会抛出以下错误:
抛出“std::out_of_range”实例后终止调用what(): vector::_M_range_check
I am working on the following code in opencv in Linux Ubuntu.
x_captured and y_captured are "int" type vectors. Size of both vectors is 18.
for (int i=0;i<=x_captured.size();i++)
{
for (int j=0;j<=y_captured.size();j++)
{
if (i!=j)
{
if (((x_captured.at(j)-x_captured.at(i))<=2) &&
((y_captured.at(j)-y_captured.at(i))<=2))
{
consecutive=consecutive+1;
}
}
}
}
But when i=0 and j=18 after that it throws the following error:
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,当有效索引为 0 到 N - 1 时,您正在使用从 0 到 N 的循环。这就是为什么您在最后一次迭代时遇到异常:
std::vector::at
执行边界检查,如果超出范围,则会抛出std::out_of_range
。您需要将循环的条件更改为
<
,而不是<=
。The problem is that you are using looping from 0 to N when valid indices are 0 to N - 1. This is why you are getting an exception on the last iteration:
std::vector::at
performs bound checking, if you are out of bounds then anstd::out_of_range
is thrown.You need to change your loop's condition to
<
, not<=
.您应该将
<=
更改为<
,然后重试。名为 Billy 的示例数组:大小:5 但最后一个索引为 4。明白了吗? :)
You should change the
<=
to<
and try again.Example array named Billy : Size : 5 but last index is 4. Get it? :)