我猜测std::hash被定义为模板结构,以避免在重载函数解析期间完成隐式类型转换.这是正确的说法吗?
我的意思是,我更愿意写
std::string s;
size_t hash = std::hash(s);
Run Code Online (Sandbox Code Playgroud)
代替
std::string s;
size_t hash = std::hash<std::string>()(s);
Run Code Online (Sandbox Code Playgroud)
但我猜测标准委员会选择第二种选择的原因是有原因的.
编辑:修复第二个代码片段.
我试图QString用作a中的键std::unordered_map,但是我得到了错误:
错误C2280:'std :: hash <_Kty> :: hash(const std :: hash <_Kty>&)':尝试引用已删除的函数
我无法切换, QHash因为地图的值类型是不可复制的.有没有办法让这项工作?
我试图专门为我自己的类型哈希,一个模板化的键.
我是基于cppreference.
我得到编译错误"C++标准不提供此类型的哈希".我想我做错了.编译器甚至可以支持这种模板吗?
namespace std {
template<typename SType, typename AType, typename PType>
struct MyKey {
const SType from;
const AType consume;
const PType pop;
};
template<typename SType, typename AType, typename PType>
struct hash<MyKey<SType, AType, PType>> {
size_t operator ()(MyKey const &key) {
std::hash<SType>()(key.from);
std::hash<AType>()(key.consume);
std::hash<PType>()(key.pop);
}
};
}
Run Code Online (Sandbox Code Playgroud) 在阅读了关于operator <for std :: weak_ptr的讨论之后,我看不出有什么理由为什么定义std :: hash来使用std :: weak_ptr的控制块是行不通的.我也无法相信标准委员会忽视了这一点.有没有人阅读有关该主题的相关讨论?
编辑:讨论运营商<for std :: weak_ptr http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1590.html
我有一个Baz包含嵌套类的模板类Sub.我想通过专门化std :: hash为这个子类定义一个哈希函数.但是,它似乎不起作用.
#include <functional>
struct Foo {
struct Sub {
};
};
template <class T>
struct Bar {
};
template <class T>
struct Baz {
struct Sub {
int x;
};
};
// declare hash for Foo::Sub - all right
namespace std {
template <>
struct hash< Foo::Sub >;
}
// declare hash for Bar<T> - all right
namespace std {
template <class T>
struct hash< Bar<T> >;
}
// declare hash function for Baz<T>::Sub …Run Code Online (Sandbox Code Playgroud) 我可以std::hash用我自己std::hash在C++ 11中的定义替换实际的实现吗?
我的意思是从我的代码库,而不是触及标准库.
在这种情况下,我看不出任何虚函数/多态的用法,所以我想我无法改变std :: hash的定义?
我已经定义了这个模板类结构:
template<typename T> struct Outer {
struct Inner { /* ...some stuff... */ };
};
Run Code Online (Sandbox Code Playgroud)
我想将Inner对象放入一个unordered_map(实际上,不是直接将它们而是它们的容器,因此直接在模板参数上指定散列对象的方法unordered_map不是一个好主意),因此我想hash为这些项目专门化类。
这将不起作用,因为编译器无法与Outer<T>::Inner实例化时指定的类型匹配hash:
namespace std {
template<typename T> struct hash<typename Outer<T>::Inner > {
size_t operator ()( typename Outer<T>::Inner const & obj )
{ /* ...some stuff... */ }
};
};
Run Code Online (Sandbox Code Playgroud)
有谁知道这个问题的解决方案?
我在我的代码中使用了TR1实现的unordered_map,并且链接器提供了奇怪的错误,我甚至无法破译:
BPCFG.o: In function `std::__detail::_Hash_code_base<DottedRule, std::pair<DottedRule const, int>, std::_Select1st<std::pair<DottedRule const, int> >, eqDottedRule, std::hash<DottedRule>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>::_M_hash_code(DottedRule const&) const':
BPCFG.cpp: (.text._ZNKSt8__detail15_Hash_code_baseI10DottedRuleSt4pairIKS1_iESt10_Select1stIS4_E12eqDottedRuleSt4hashIS1_ENS_18_Mod_range_hashingENS_20_Default_ranged_hashELb0EE12_M_hash_codeERS3_[std::__detail::_Hash_code_base<DottedRule, std::pair<DottedRule const, int>, std::_Select1st<std::pair<DottedRule const, int> >, eqDottedRule, std::hash<DottedRule>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>::_M_hash_code(DottedRule const&) const]+0x23): undefined reference to `std::hash<DottedRule>::operator()(DottedRule) const'
BPCFG.o: In function `std::__detail::_Hash_code_base<DottedRule, std::pair<DottedRule const, int>, std::_Select1st<std::pair<DottedRule const, int> >, eqDottedRule, std::hash<DottedRule>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>::_M_bucket_index(std::__detail::_Hash_node<std::pair<DottedRule const, int>, false> const*, unsigned long) const':
BPCFG.cpp: (.text._ZNKSt8__detail15_Hash_code_baseI10DottedRuleSt4pairIKS1_iESt10_Select1stIS4_E12eqDottedRuleSt4hashIS1_ENS_18_Mod_range_hashingENS_20_Default_ranged_hashELb0EE15_M_bucket_indexEPKNS_10_Hash_nodeIS4_Lb0EEEm[std::__detail::_Hash_code_base<DottedRule, std::pair<DottedRule const, int>, std::_Select1st<std::pair<DottedRule const, int> >, eqDottedRule, std::hash<DottedRule>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>::_M_bucket_index(std::__detail::_Hash_node<std::pair<DottedRule const, int>, false> …Run Code Online (Sandbox Code Playgroud) 您好,我对 std::hash 有一个问题,如果我有 2 个大字符串进行比较,并且我愿意接受 std::hash 在大多数情况下比较相等,那么执行类似以下操作而不是直接执行操作是否更符合性能字符串比较?还要考虑这将在循环中读取文件,因此它将被执行多次,这是大文件的问题。
std::string largeString1; // large but not huge meaning a line of text like up to lets say 500 chars
std::string largeString2;
// is this better than then next block in terms of performance and if so by how much?
if ( std::hash<std::string>(largeString1) == std::hash<std::string>(largeString2) )
{
// true logic
}
// is this a lot slower than the previous
if ( largeString1 == largeString2 )
{
// true logic
}
Run Code Online (Sandbox Code Playgroud)