Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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_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 ===

like image 714
pandoragami Avatar asked Sep 03 '25 04:09

pandoragami


1 Answers

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!

like image 70
templatetypedef Avatar answered Sep 04 '25 17:09

templatetypedef