我有以下代码,我想在其中消除我创建的初始值为 10 的元素。我在设置迭代器和删除它时遇到了问题。它是怎么做到的?
#include <iostream>
#include <boost/unordered_map.hpp>
using namespace std;
int main()
{
typedef boost::unordered_map<int, boost::unordered_map<int, boost::unordered_map<int, int> > >::const map_it;
typedef boost::unordered_map<int, boost::unordered_map<int, boost::unordered_map<int, int> > > _map;
_map _3d;
_3d[0][0][0] = 10;
cout<<_3d[0][0][0]<<endl;
map_it = _3d[0][0][0].begin();
_3d[0][0][0].erase(map_it);
return 0;
}
multimapBoost.cpp||In function 'int main()':|
multimapBoost.cpp|16|error: expected unqualified-id before '=' token|
multimapBoost.cpp|18|error: request for member 'erase' in '((boost::unordered_map<int, int, boost::hash<int>, std::equal_to<int>, std::allocator<std::pair<const int, int> > >*)((boost::unordered_map<int, boost::unordered_map<int, int, boost::hash<int>, std::equal_to<int>, std::allocator<std::pair<const int, int> > >, boost::hash<int>, std::equal_to<int>, std::allocator<std::pair<const int, boost::unordered_map<int, int, boost::hash<int>, std::equal_to<int>, std::allocator<std::pair<const int, int> > > > > >*)_3d.boost::unorder|
multimapBoost.cpp|18|error: expected primary-expression before ')' token|
||=== Build finished: 3 errors, 0 warnings ===|
最佳答案
你有太多[0]
:
_3d[0][0][0].begin();
// should be:
_3d[0][0].begin();
此外,map_it
是类型,不是变量;你需要声明一个 map_it
类型的变量并分配给或初始化该变量。
_3d[0][0].begin()
的类型就是boost::unordered_map<int, int>::iterator
(或 const_iterator
); _3d.begin()
的类型将是您似乎要尝试使用的嵌套迭代器类型。
一些额外的 typedef 将使这段代码更加简单。
关于c++ - 从多级 unordered_map 中删除元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6785894/