从多级 unordered_map 中删除元素?
我有以下代码,我想消除我最初创建的值为 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 ===|
I have the following code where I wanted to eliminate the element I created initialy with the value 10. I'm having trouble setting up an iterator and erasing it. How is it done?
#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 ===|
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
[0]
过多:此外,
map_it
是一种类型,而不是变量;您需要声明一个map_it
类型的变量并分配或初始化该变量。_3d[0][0].begin()
的类型只是boost::unordered_map::iterator
(或const_iterator
代码>);_3d.begin()
的类型将是您似乎尝试使用的嵌套迭代器类型。一些额外的 typedef 将使这段代码变得更加简单。
You have one too many
[0]
:Further,
map_it
is a type, not a variable; you need to declare a variable of typemap_it
and assign to or initialize that variable.The type of
_3d[0][0].begin()
is simplyboost::unordered_map<int, int>::iterator
(orconst_iterator
); the type of_3d.begin()
would be the nested iterator type that you seem to be trying to use.A few additional typedefs would make this code very much straightforwarder.