unordered_multimap element insert
I tried to insert a value into a boost unordered_multimap using the following code but that didn't work since it doesnt compile. Why is there no access [] operator?
The insert() method doesn't work either?
#include <iostream>
#include <boost/unordered_map.hpp>
using namespace std;
int main()
{
typedef boost::unordered_multimap<
int,
boost::unordered_multi开发者_C百科map<
int,
boost::unordered_multimap<int, int>
>
> unordered_map;
unordered_map _3d;
_3d[0][0][0] = 10;
_3d[0][0][0].insert(10);
cout << _3d[0][0][0] << endl;
}
Errors:
multimap.cpp||In function
multimap.cpp|19|error: no match for'int main()'
:'operator[]'
in'_3d[0]'
multimap.cpp|21|error: no match for'operator[]'
in'_3d[0]'
||=== Build finished: 2 errors, 0 warnings ===
Neither unordered_multimap
nor multimap
supports operator[]
because it's not well-defined what it would mean. The idea behind operator[]
is that you could write myMap[key]
to mean "the unique value associated with key
in myMap
." When dealing with a unordered_multimap
, this isn't mathematically well-defined; there can be many key-value pairs with the same key in the multimap, and it's unclear which particular value you'd like to have.
From the example you gave, it seems like you really want to have a standard unordered_map
rather than an unordered_multimap
. If you're trying to use a map of map of maps to represent some higher-dimensional array, then you probably only want to have each coordinate map to one particular subspace. Replacing unordered_multimap
with unordered_map
should fix this.
Hope this helps!
boost::unordered_map
doesn't appear to have operator[]
. Use the lookup functions instead.
What reference are you using?
精彩评论