简体   繁体   中英

c++ map/set iterator not dereferencable using .find

I've relatively new to using maps, and I'm currently getting the Debug Assertion Failed Expression: map/set iterator not dereferencable

When I hit retry it brings me to this section of code:

auto temp = mOpenMap.find(currentNode); temp->second = false;

I assume this has to do with the .find(currentNode) returning the end of the map, as it didn't find it, but the concerning part here is that doing my manual debugging I found that the only Node in the map indeed contained the exact parts of the currentNode I had it search for.

My map is this:

std::map<PathNode*, bool> mOpenMap

Optimistically what I would like it to do is search for the row and column to ascertain that it is looking at a node that has already been searched so that I can set the accompanying boolean to false.

What I'm wondering, is how do maps generally search for objects? Or better yet, how can I go about making the map search with a custom search?

You should check std::map::find does find the element before dereference the iterator:

auto temp = mOpenMap.find(currentNode);
if (temp != mOpenMap.end())  // check temp is pointing to underneath element of a map
{
    temp->second = false;
}

If all you're doing is tracking the presence of some PathNode or other, you'd be better off using a std::set .

As for a custom search, both std::map and std::set work with a collection of values, ordered by a comparator. That comparator can be specified as the second template type when the map or set is defined. If omitted, that comparator defaults to std::less , which simply compares the objects with the less than operator, operator< . As written, your map mOpenMap is using the value of the pointer to perform this comparison, which is probably not what you want.

I suggest you declare and define PathNode::operator< , and replace mOpenMap with a member of type std::set<PathNode> . This will key off of actual PathNode values, rather than pointers (which will probably never collide under normal circumstances).

Remember that your PathNode::operator< should generate a strict ordering of PathNode objects. This is a requirement of the comparator for std::map and std::set. if you don't follow this rule, it will behave erratically, but it will compile and run, so make sure you pay attention to this detail.

You should check the result as billz said. The most likely reason that the find failed is that your map is keyed on PathNode * meaning that it will only find nodes with an exact pointer match. Searching for a pathnode with the same member values as one in the map will not work.

If you need the map to be on PathNode * , then you will need to also supply a predicate as the third parameter of the map. The predicate will need to be written to compare two PathNode * parameters by their member values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM