I want to use map in C++ STL to create assosiation between vector and int. But I got multiple compilation error with code presented below:
#include <vector>
#include <map>
#include <unordered_map>
using namespace std;
int main(void)
{
unordered_map<vector<char>, int> mp;
return 0;
}
And got this compilation error in VC++:
error C2280: 'std::hash<_Kty>::hash(const std::hash<_Kty> &)': attempting to reference a deleted function
However, if I change my code like presented below, then the code can be compiled properly:
#include <vector>
#include <map>
#include <unordered_map>
using namespace std;
int main(void)
{
map<vector<char>, int> mp;
return 0;
}
I have found this question asked in StackoverFlow, whose title is: C++ unordered_map using a custom class type as the key. But I wondered that why using map<> can pass the compilation check but unable with unordered_map<> ?
map
requires that less-than comparison is implemented. Which it is, for a vector. But unordered_map
requires a hash function, which you'll need to implement yourself. It's not a big deal, you can see how to use hash_combine
to do that here: Fast hash function for `std::vector`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With