当前位置: 首页 > 知识库问答 >
问题:

unordered_map无法在工作的C++中初始化?

施永宁
2023-03-14

问题:给定一个字符串数组,将字母表组合在一起。你可以按任何顺序返回答案。

字形是一个单词或短语,通过重新排列一个不同的单词或短语的字母,通常使用所有的原始字母一次。

Example 1:

Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]

代码:

class Solution
{
public:
    vector<vector<string>> groupAnagrams(vector<string> &strs)
    {
        unordered_map <unordered_multiset <char>, vector<string>> mp1;
        for (c : strs)
        {
            unordered_multiset<char> ms1;
            for (char h : c)
                m1.insert(h);
            mp1[ms1].push_back(c);
        }
        vector<vector<string>> res;
        for (auto c : mp1)
            res.push_back(c.second);
        return res;
    }
};

错误:

Line 6: Char 67: error: call to implicitly-deleted default constructor of 'unordered_map<unordered_multiset<char>, vector<std::string>>' (aka 'unordered_map<unordered_multiset<char>, vector<basic_string<char>>>')
        unordered_map <unordered_multiset <char>, vector<string>> mp1;
                                                                  ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unordered_map.h:141:7: note: explicitly defaulted function was implicitly deleted here
      unordered_map() = default;
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unordered_map.h:105:18: note: default constructor of 'unordered_map<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>, std::vector<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char>>>, std::hash<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>, std::equal_to<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>, std::allocator<std::pair<const std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>, std::vector<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char>>>>>>' is implicitly deleted because field '_M_h' has a deleted default constructor
      _Hashtable _M_h;
                 ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable.h:414:7: note: explicitly defaulted function was implicitly deleted here
      _Hashtable() = default;
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable.h:174:7: note: default constructor of '_Hashtable<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>, std::pair<const std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>, std::vector<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char>>>>, std::allocator<std::pair<const std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>, std::vector<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char>>>>>, std::__detail::_Select1st, std::equal_to<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>, std::hash<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true>>' is implicitly deleted because base class '__detail::_Hashtable_base<unordered_multiset<char, hash<char>, equal_to<char>, allocator<char>>, pair<const unordered_multiset<char, hash<char>, equal_to<char>, allocator<char>>, vector<basic_string<char>, allocator<basic_string<char>>>>, _Select1st, equal_to<unordered_multiset<char, hash<char>, equal_to<char>, allocator<char>>>, hash<unordered_multiset<char, hash<char>, equal_to<char>, allocator<char>>>, _Mod_range_hashing, _Default_ranged_hash, _Hashtable_traits<true, false, true>>' has a deleted default constructor
    : public __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable_policy.h:1822:5: note: explicitly defaulted function was implicitly deleted here
    _Hashtable_base() = default;
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable_policy.h:1771:5: note: default constructor of '_Hashtable_base<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>, std::pair<const std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>, std::vector<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char>>>>, std::__detail::_Select1st, std::equal_to<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>, std::hash<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true>>' is implicitly deleted because base class '_Hash_code_base<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>, std::pair<const std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>, std::vector<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char>>>>, std::__detail::_Select1st, std::hash<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, _Hashtable_traits<true, false, true>::__hash_cached::value>' has a deleted default constructor
  : public _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable_policy.h:1373:7: note: explicitly defaulted function was implicitly deleted here
      _Hash_code_base() = default;
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable_policy.h:1349:7: note: default constructor of '_Hash_code_base<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>, std::pair<const std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>, std::vector<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char>>>>, std::__detail::_Select1st, std::hash<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>' is implicitly deleted because base class '_Hashtable_ebo_helper<1, std::hash<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>>' has a deleted default constructor
      private _Hashtable_ebo_helper<1, _H1>,
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable_policy.h:1096:7: note: explicitly defaulted function was implicitly deleted here
      _Hashtable_ebo_helper() = default;
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable_policy.h:1094:7: note: default constructor of '_Hashtable_ebo_helper<1, std::hash<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>, true>' is implicitly deleted because base class 'std::hash<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>' has a deleted default constructor
    : private _Tp
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/functional_hash.h:101:19: note: default constructor of 'hash<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>' is implicitly deleted because base class '__hash_enum<std::unordered_multiset<char, std::hash<char>, std::equal_to<char>, std::allocator<char>>>' has no default constructor
    struct hash : __hash_enum<_Tp>
                  ^

共有2个答案

连德水
2023-03-14

不能将unordered_multiset原样用作unordered_map中的键,因为它没有std::hash专门化。

您可以专门化std::hash或为unordered_multiset提供自己的哈希函数。

但这些实际上都不需要,因为您可以使用排序的字符串作为键:

vector<vector<string>> groupAnagrams(vector<string> &strs)
{
    map <string, vector<string>> mp1;
    for (auto &s : strs) {
        string sc = s;
        sort(begin(sc), end(sc));
        mp1[sc].push_back(s);
    }
    vector<vector<string>> res;
    for (auto &c : mp1) {
        res.push_back(c.second);
    }
    return res;
}
辛承志
2023-03-14

unordered_map要求密钥类型有一个哈希函数。在本例中,它是unordered_multiset ,但没有unordered_multiset std::hash专门化。您将需要制作您自己的收割机,以便使用这个。

这里的错误信息并不是最大的,但这是暗指的。

 类似资料:
  • 我试图在一个课程中使用xampp,但我无法使tomcat正常工作,我得到的错误是: Tomcat启动/停止时出现错误,返回代码:1确保安装了Java JDK或JRE,并且所需端口空闲,有关详细信息,请检查“/xampp/tomcat/logs”文件夹 我见过很多有这个问题的人,但他们得到的建议都没有帮助我摆脱它。 我已经确定了环境变量如下所示: CATALINA_HOME:C:\XAMPP\Tom

  • 以下是MyService应用程序: 我已经指定了@AllargsConstructor。当我试图为上面的类编写单元测试时,它失败了。以下是我如何编写单元测试: 抛出的错误是: Java:类中的构造函数MyService不能应用于给定类型;必填:未找到参数:原因:实际参数列表和正式参数列表的长度不同 但当我在MyService中显式定义构造函数时,测试就会通过。这里有人能帮我吗?

  • 问题内容: 我编写了上面的代码,以提供更方便的方式来声明自定义uicolor。但是以某种方式,它通过调用自身直到堆栈溢出而使我的应用程序崩溃。怎么了 另外,我只是意识到我没有明确调用此init函数。但是,当发生此错误时,我正在打电话。当然,如果我显式调用此函数,仍然会发生错误! 问题答案: 我认为这样做不会带来任何不便: 但是如果您真的需要一个,可以按照以下步骤操作:

  • 我正在尝试使用Spring开发Rest代码。 我的MongoDAO类如下所示 但我得到的错误是.. 原因:org . spring framework . beans . factory . beancreationexception:创建在文件[C:\ Users \ user \ Desktop \ ITAU \ 77 \ ABC-iCOE-WAL \ ItauBankingTransac s

  • 我有一个使用MIP SDK的Azure函数,当调用< code>MIP时,我得到以下错误。CreateMipContext(...): 令人惊讶的是,该错误仅在 Azure 上运行时发生。在本地运行时,一切都很好。 我正在使用MIP SDK v1.8.86和.NET Core 3.1。 知道OneDS是什么或者是什么导致了错误吗?

  • 新CONF:我做了一些改变来配合你所说的。现在我删除了hibernate-cfg.xml并在spring-servlet.xml中做了所有的conf: 它可能来自我的java文件吗? 错误是: Bean 类 [org.springframework.orm.hibernate4.LocalSessionFactoryBean] 的属性 'transaction' 无效:Bean 属性 'trans