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));
}
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.
Before accessing edges[j]
check that j < edges.size()
.
EDIT:
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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With