-3

I have a vector of Node*(openList) and I was hoping to get an iterator to the the Node* when a targetNode is given. It should check the x & z position and if a match is found, i would know that this Node* already exists. This is what I have

auto iter = std::find_if(openList.begin(), openList.end(), [&targetNode](const Node* p)
                {
                  return (targetNode->GetXPos() == p->GetXPos() && targetNode->GetZPos() == p->GetZPos());
                });
if (iter != openList.end())
{
  ....do something
}

When I step through Visual Studio, I noticed that there are multiple same entries in openList that is not being caught by the lambda expression. Could anyone enlighten me as to what I had done wrong since I am never entering the do-something loop at all? Thanks alot.

1 Answers1

1

There is nothing wrong with that code. The problem probably lies in the comparison between whatever GetXPos() and GetZPos() return.

If they are integers, it should be fine and the code should work.

If they are doubles or floats, you must use some other way of comparing them (see this StackOverflow question).

If they are objects implemented by you, double check how they are determined to be equal (your implementation of operator== for those objects).

user2891462
  • 3,033
  • 2
  • 32
  • 60