Questions tagged «unordered-set»

3
如何在无序容器中将std :: hash <Key> :: operator()专用于用户定义的类型?
为了支持用户定义的键类型std::unordered_set&lt;Key&gt;和std::unordered_map&lt;Key, Value&gt; 一个具有提供operator==(Key, Key)和散列函子: struct X { int id; /* ... */ }; bool operator==(X a, X b) { return a.id == b.id; } struct MyHash { size_t operator()(const X&amp; x) const { return std::hash&lt;int&gt;()(x.id); } }; std::unordered_set&lt;X, MyHash&gt; s; 仅std::unordered_set&lt;X&gt; 使用type 的默认哈希值编写将更方便X,例如与编译器和库一起提供的类型。经过咨询 C ++标准草案N3242§20.8.12 [unord.hash]和§17.6.3.4[hash.requirements], 增强无序 g ++ include\c++\4.7.0\bits\functional_hash.h …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.