unordered_map的适当价值

Proper value for unordered_map

本文关键字:map unordered      更新时间:2023-10-16

我有一组字符串,我必须将它们放入哈希表中并检索它的字谜。我选择了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;
    }
}