开发者

Erasing element from multilevel unordered_map?

开发者 https://www.devze.com 2023-03-22 07:54 出处:网络
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?

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;
开发者_JAVA技巧
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 ===|


You have one too many [0]:

_3d[0][0][0].begin(); 
// should be:
_3d[0][0].begin();

Further, map_it is a type, not a variable; you need to declare a variable of type map_it and assign to or initialize that variable.

The type of _3d[0][0].begin() is simply boost::unordered_map<int, int>::iterator (or const_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.

0

精彩评论

暂无评论...
验证码 换一张
取 消