我理解一个集合是有序的,因此添加一个对象而不会重载<操作符不允许说哪个对象更小以保持容器排序.但是,我不明白为什么这是不可能的unordered_set.
如果我尝试这样的事情:
#include <iostream>
#include <string
#include <unordered_set>
struct someType{
string name;
int code;
};
int main(){
std::unordered_set <someType> myset;
myset.insert({"aaa",123});
myset.insert({"bbb",321});
myset.insert({"ccc",213});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到了一些错误:
c:\ qt\qt5.1.0\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c ++\bits\hashtable_policy.h:1070:错误:无效使用不完整类型'struct std: :哈希"
c:\ qt\qt5.1.0\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c ++\bits\functional_hash.h:58:错误:'struct std :: hash'的声明
错误:没有匹配函数来调用'std :: unordered_set :: unordered_set()'
c:\ qt\qt5.1.0\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c ++\bits\hashtable_policy.h:1103:错误:无法匹配调用'(const std :: hash)(const someType&)'
c:\ qt\qt5.1.0\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c ++\bits\stl_function.h:208:错误:不匹配'operator =='(操作数类型是'const someType'和'const someType')
为什么这样,我该如何解决?