简体   繁体   English

使用.find无法引用c ++ map / set迭代器

[英]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 我对使用地图还比较陌生,目前正在获取“ 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. 我认为这与.find(currentNode)返回地图的末尾有关,因为它没有找到它,但是这里有关的部分是进行手动调试时,我发现地图中唯一的Node确实包含我要搜索的currentNode的确切部分。

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. 乐观地说,我想做的是搜索rowcolumn以确保它正在查看已被搜索的节点,因此我可以将随附的boolean设置为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: 您应该检查std::map::find在取消引用迭代器之前是否找到了元素:

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 . 如果您正在做的只是跟踪某个PathNode或其他PathNode的存在,那么最好使用std::set

As for a custom search, both std::map and std::set work with a collection of values, ordered by a comparator. 对于自定义搜索, std::mapstd::set可以使用由比较器排序的值的集合。 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< . 如果省略,则该比较器默认为std::less ,它仅将对象与小于运算operator< As written, your map mOpenMap is using the value of the pointer to perform this comparison, which is probably not what you want. 如所写,您的地图mOpenMap使用指针的值来执行此比较,这可能不是您想要的。

I suggest you declare and define PathNode::operator< , and replace mOpenMap with a member of type std::set<PathNode> . 我建议您声明并定义PathNode::operator< ,并将mOpenMap替换为std::set<PathNode>类型的成员。 This will key off of actual PathNode values, rather than pointers (which will probably never collide under normal circumstances). 这将取消实际的PathNode值,而不是指针(在正常情况下可能永远不会发生冲突)。

Remember that your PathNode::operator< should generate a strict ordering of PathNode objects. 请记住,您的PathNode::operator<应该生成严格的PathNode对象排序 This is a requirement of the comparator for std::map and std::set. 这是比较器对std :: map和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. 您应该按照Billz所说的检查结果。 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. 查找失败的最可能原因是您的地图在PathNode *上进行了键控,这意味着它只会找到指针完全匹配的节点。 Searching for a pathnode with the same member values as one in the map will not work. 搜索具有与映射中的成员值相同的成员值的pathnode将不起作用。

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. 如果需要将地图放在PathNode * ,那么还需要提供predicate作为地图的第三个参数。 The predicate will need to be written to compare two PathNode * parameters by their member values. 将需要编写predicate来比较两个PathNode *参数的成员值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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