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<> ?
std::map
uses strict weak ordering and by-defaultstd::less
(which will implicitly by-default use some flavor ofoperator <
). That is defined for vectors of comparable entities (such aschar
).std::unordered_map
usesstd::hash
, and is not defined for your key type (std::vector<char>
). So either define it or provide your own hashing override type. – WhozCraig