检查向量索引是否为空 [英] check to see if vector index is empty
本文介绍了检查向量索引是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我需要这样做:
In my code, I need to do this:
if (edges[j].ConnectedToNode() != i) //problem line
{
edges.push_back(Edge(i, j, nodes[i].Position(), nodes[j].Position(), distanceToNode));
}
然而,edge[j] 可能还不存在.如何对此进行测试以避免和索引超出范围的异常?(这与路径节点有关,本质上如果有一条边将 j 连接到 i,我不想在 i 到 j 之间添加另一个边.
however, there is a possibility that edges[j] does not exist yet. how can I test for this to avoid and index out-of-range exception? (This is to do with path nodes, essentially if there is an edge connecting j to i, I don't want to add another from i to j.
推荐答案
在访问 edges[j]
之前检查 j
Before accessing edges[j]
check that j < edges.size()
.
为了说明 Mark Ransom 的评论:
To illustrate what Mark Ransom commented:
if (j < edges.size() && edges[j].ConnectedToNode() != i) //problem line
{
edges.push_back(Edge(i, j, nodes[i].Position(), nodes[j].Position(), distanceToNode));
}
这篇关于检查向量索引是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文