0
Object comparison?
So in this code, I have a vector(nodes) containing the nodes for each grid. When you delete a node, it needs to remove it from the vector. But I'm having trouble with the comparison. I need it to compare the node given via function arg and the node at nodes[i], and if they're the same, remove it. How can I make this work? https://code.sololearn.com/cIVz1q45no78/?ref=app
1 RĂ©ponse
0
To achieve object comparison for removing a specific node from the vector, you can update your delete_node function to compare the memory addresses of the objects rather than their values. Here's how you can do that in your delete_node function:
void delete_node(node *n) {
if (n->row + 1 > 0 && n->column + 1 > 0)
data[n->row][n->column] = '+';
for (auto it = nodes.begin(); it != nodes.end(); ++it) {
if (*it == n) {
nodes.erase(it);
delete *it; // Remember to deallocate the memory to prevent memory leaks
break; // Exit the loop once the node is found and removed
}
}
}