unordered_map的适当价值
Proper value for unordered_map
我有一组字符串,我必须将它们放入哈希表中并检索它的字谜。我选择了unordered_map,因为它是 c++ 中的内置哈希表。字符串如下所示,
cat, act, art, tar, rat... etc..
现在我使用按字母顺序排序的单词作为键,使用无序单词的向量作为值。此实现在插入时需要花费大量时间。这是满足要求的最佳实现方式,还是可以使用更少耗时的东西?
std::tr1::unordered_map<std::string, std::vector<std::string>> map;
if(map.find(sortedWord) == map.end()){
std::vector<std::string> temp;
temp.push_back(word);
map.insert(make_pair(sortedWord, temp));
}else{
map.find(sortedWord)->second.push_back(word);
}
你使它变得比必要的复杂得多,在这个过程中,你也通过以下方式减慢了速度:
- 搜索密钥两次,以及
- 当您有新密钥时复制向量(包含的单词)。事实上,它可能被复制了两次。
以下内容适用于 C++11,我很确定它与 tr1 的工作方式相同:
/* Name of map changed because I don't like to use "map"
* or "string" as variable names */
wordMap[sortedWord].push_back(word);
如果地图中不存在sortedWord
,则wordMap[sortedWord]
将使用默认构造的std::vector<std::string>>
插入它。因此,无论sortedWord
是否存在,新单词都可以附加到下标返回的值上。
只是为了提供另一种解决方案,您可以使用带有自定义哈希算法和相等比较的 C++11 std::unordered_multiset
。
自定义哈希算法可以简单地将每个字符的哈希值与交换操作(例如按位异或)组合在一起,以便所有字谜都具有相同的哈希值。
自定义相等比较可以使用std::is_permutation
来等同所有字谜。
struct AnagramHash
{
typedef std::string argument_type;
typedef std::hash<char>::result_type result_type;
result_type operator()(const argument_type& s) const
{
std::hash<char> char_hash;
result_type result = 0;
for (const auto& x : s)
{
result ^= char_hash(x);
}
return result;
}
};
struct AnagramEqual
{
typedef bool result_type;
typedef std::string first_argument_type;
typedef std::string second_argument_type;
result_type operator()(const first_argument_type& lhs, const second_argument_type& rhs) const
{
if (lhs.size() == rhs.size())
return std::is_permutation(std::begin(lhs), std::end(lhs), std::begin(rhs));
else
return false;
}
};
int main()
{
std::unordered_multiset<std::string, AnagramHash, AnagramEqual> anagrams;
anagrams.insert("arc");
anagrams.insert("rac");
anagrams.insert("car");
anagrams.insert("H2O");
anagrams.insert("2OH");
auto range = anagrams.equal_range("car");
for (auto it = range.first ; it != range.second ; ++it)
{
cout << *it << endl;
}
cout << endl;
range = anagrams.equal_range("HO2");
for (auto it = range.first ; it != range.second ; ++it)
{
cout << *it << endl;
}
}
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 使用一个考虑到std::map中键值的滚动或换行的键
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 允许从 std::map 的密钥窃取资源?
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用通用值初始化 std::map,不重复
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++:当所有条目都保证是唯一时,替代 std::map
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- 使用字符数组作为 Map 中的键
- C++如何创建 std::map
- C++ equivalent to Java Map getOrDefault?
- 从其他容器中移动构造"std::map"
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?
- C++中 std::map 的运行时复杂度是多少?
- unordered map -在c++ std::unordered_map中预分配桶