0

I was trying and got to the following code

#include <unordered_map>
#include <vector>
#include <string>
using namespace std;

int main() {
    unordered_map<char, int> myMap;
    vector<string> myVector;
    unordered_map<unordered_map<char, int>, vector<string>> myUnorderedMap;
    myUnorderedMap[myMap] = myVector;
    return 0;
}

I get the following errors:

main.cpp: In function ‘int main()’:
main.cpp:9:61: error: use of deleted function ‘std::unordered_map\<\_Key, \_Tp, \_Hash, \_Pred, \_Alloc\>::unordered_map() \[with \_Key = std::unordered_map; \_Tp = std::vector \>; \_Hash = std::hash \>; \_Pred = std::equal_to \>; \_Alloc = std::allocator, std::vector \> \> \>\]’
9 |     unordered_map\<unordered_map\<char, int\>, vector\<string\>\> myUnorderedMap;
|                                                             ^\~\~\~\~\~\~\~\~\~\~\~\~\~
In file included from /usr/include/c++/11/unordered_map:47,
from main.cpp:1:

I am trying to create an unordered_map of an unordered map and a vector of strings in c++ something like that unordered_map<unordered_map<char, int>, vector<string>> myUnorderedMap;

cigien
  • 57,834
  • 11
  • 73
  • 112
  • 2
    In order to be able to use `unordered_map` as a key in a hash map, you need to be able to compute its hash. If you really want to go down that path, implement your hashing class and pass it as the third template parameter when declaring `myUnorderedMap`. The standard library doesn't provide such a hash function (because, frankly, it makes little sense). – Igor Tandetnik Jun 04 '23 at 01:04
  • 2
    Does this answer your question? [C++ unordered\_map using a custom class type as the key](https://stackoverflow.com/questions/17016175/c-unordered-map-using-a-custom-class-type-as-the-key) – cigien Jun 04 '23 at 07:50
  • 1
    A map as a key to another map doesn't make a lot of sense. Do you have a real world use case for this? – n. m. could be an AI Jun 04 '23 at 10:21

0 Answers0