unordered_map中的字符串的C ++哈希函数 [英] C++ Hash function for string in unordered_map
本文介绍了unordered_map中的字符串的C ++哈希函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看起来C ++对标准库中的字符串没有散列函数。这是真的吗?
It seems as if C++ does not have a hash function for strings in the standard library. Is this true?
在使用任何c ++编译器的unordered_map中使用字符串作为键的工作示例是什么?
What is a working example of using a string as a key in an unordered_map that will work with any c++ compiler?
推荐答案
C ++ STL提供了特殊化< a> std :: hash
。您可以指定 std :: string
作为 std :: unordered_map
的键类型:
C++ STL provides template specializations of std::hash
for the various string classes. You could just specify std::string
as key type for std::unordered_map
:
#include <string>
#include <unordered_map>
int main()
{
std::unordered_map<std::string, int> map;
map["string"] = 10;
return 0;
}
这篇关于unordered_map中的字符串的C ++哈希函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文