Cor*_*lor 5 c++ hash unordered-map c++-standard-library c++11
在阅读用于std :: unordered_map的std :: hash示例时,我注意到{}正在访问operator()函数.
http://en.cppreference.com/w/cpp/utility/hash
result_type operator()(argument_type const& s) const
{
result_type const h1 ( std::hash<std::string>{}(s.first_name) );
result_type const h2 ( std::hash<std::string>{}(s.last_name) );
return h1 ^ (h2 << 1); // or use boost::hash_combine (see Discussion)
}
Run Code Online (Sandbox Code Playgroud)
这里使用{}代表什么?
std::hash<T> 是一种不是功能的类型.
的实例std::hash有operator(),做的哈希值.
std::hash<std::string>哈希类型也是如此. {}然后创建该类型的实例. (s.first_name)打电话operator()给std::hash<std::string>.
std::hash<std::string>{}(s.first_name);
^ ^ ^
| | call operator() on that instance
type of hasher |
create an instance of that type
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
451 次 |
| 最近记录: |