Object comparison? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

10th Aug 2019, 8:26 PM
Daniel Cooper
1 Answer
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 } } }
16th Apr 2024, 4:23 AM
`нттp⁴⁰⁶
`нттp⁴⁰⁶ - avatar