我有一个多重映射,其键很短,值是另一个多重映射
std::multimap<short,std::multimap<short,short>> multimap
现在我想做`
std::multimap<short,short> &a=multimap.find(0)->second;
std::pair<short,short> z={1,2};
a.insert(z);
它编译得很好。但是当我运行它时它只是停止并且没有完成该过程,它甚至不会抛出任何运行时错误。有什么想法吗?谢谢建议。
最佳答案
有
std::multimap<short,std::multimap<short,short>> multimap
...
std::multimap<short,short> &a=multimap.find(0)->second;
std::pair<short,short> z={1,2};
a.insert(z);
如果 find
返回 multimap::end
,则其中一个不应被取消引用,但您这样做并获得对 second
的引用,该行为稍后使用对 insert
的引用时未定义。
所以当然要检查find
是否成功,例如
std::multimap<short,std::multimap<short,short>> multimap;
std::multimap<short,std::multimap<short,short>>::iterator it = multimap.find(0);
if (it == multimap.end()) {
...
}
else {
std::multimap<short,short> &a = it->second;
std::pair<short,short> z={1,2};
a.insert(z);
}
<小时/>
您的标题“编译器未完成进程”不是很清楚,执行不是您期望的,但编译器没有运行该进程
关于c++ - 编译器未完成进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61368590/