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_multimap<
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
'int main()':
multimap.cpp|19|error: no match for'operator[]'in'_3d[0]'
multimap.cpp|21|error: no match for'operator[]'in'_3d[0]'
||=== Build finished: 2 errors, 0 warnings ===
Neither
unordered_multimapnormultimapsupportsoperator[]because it’s not well-defined what it would mean. The idea behindoperator[]is that you could writemyMap[key]to mean “the unique value associated withkeyinmyMap.” When dealing with aunordered_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_maprather than anunordered_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. Replacingunordered_multimapwithunordered_mapshould fix this.Hope this helps!