【问题标题】:Why am I getting this error as part of hashing?为什么我在散列中收到此错误?
【发布时间】:2021-09-23 15:35:23
【问题描述】:

我正在解决关于 leetcode 的this 问题。

这是我的代码,我基本上在其中创建了一个邻接列表并执行 bfs 并在途中计算结果。 (不要在逻辑和方法上添加太多细节,因为我得到的错误与 unordered_map 数据结构的使用更相关):

public:
    vector<double> calcEquation(vector<vector<string>>& eq, vector<double>& val, vector<vector<string>>& queries) 
{
        // create AL
        unordered_map<string, vector<pair<string, double>> > al;
        int n = eq.size();
        int number_nodes = 0;
        for(int i = 0; i < n; ++i)
        {
            auto pair = eq[i];
            al[pair[0]].push_back({pair[1], val[i]});
            al[pair[1]].push_back({pair[0], 1/val[i]});
            number_nodes += 2;
        }
        n = queries.size();
        // check if node is visited
        unordered_map<string, int> vis(number_nodes, 0); // 0=>unvisi
        vector<double>ans;
        for(int i = 0; i < n; ++i)
        {
            string src = queries[i][0];
            string dst = queries[i][1];
            
            // bfs
            queue<pair<string, double>> q;
            q.push({src, 1});
            // mark as vis
            vis[src] = 1;
            int flag = 0;
            while(!q.empty())
            {
                auto curr = q.front();
                q.pop();
                string node = curr.first;
                double val = curr.second;
                // iterate neighs
                for(auto neigh: al[node])
                {
                    string node1 = neigh.first;
                    double val1 = neigh.second;
                    if(!vis[node1])
                    {
                        int calc_result = val1 * val;
                        if(node1 == dst)
                        {
                            ans.push_back(calc_result);
                            flag = 1;
                            cout << val1 << endl;
                            break;
                        }
                        
                        q.push({node1, calc_result});
                        vis[node1] = 1;                             
                    }  
                }
                if(flag) // dst reached stop bfs
                {
                    break;
                }
            }
            ans.push_back(-1);
        }
        return ans;
    }       
};

但我收到以下编译时错误:

In file included from prog_joined.cpp:1:
In file included from ./precompiled/headers.h:34:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/algorithm:71:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/pstl/glue_algorithm_defs.h:13:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/functional:61:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/unordered_map:46:
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable.h:981:20: error: no matching function for call to '__distance_fw'
        auto __nb_elems = __detail::__distance_fw(__f, __l);
                          ^~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable.h:456:4: note: in instantiation of function template specialization 'std::_Hashtable<std::__cxx11::basic_string<char>, std::pair<const std::__cxx11::basic_string<char>, int>, std::allocator<std::pair<const std::__cxx11::basic_string<char>, int>>, std::__detail::_Select1st, std::equal_to<std::__cxx11::basic_string<char>>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true>>::_Hashtable<int>' requested here
        : _Hashtable(__f, __l, __n, __hf, _H2(), _Hash(), __eql,
          ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unordered_map.h:177:4: note: in instantiation of function template specialization 'std::_Hashtable<std::__cxx11::basic_string<char>, std::pair<const std::__cxx11::basic_string<char>, int>, std::allocator<std::pair<const std::__cxx11::basic_string<char>, int>>, std::__detail::_Select1st, std::equal_to<std::__cxx11::basic_string<char>>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true>>::_Hashtable<int>' requested here
        : _M_h(__first, __last, __n, __hf, __eql, __a)
          ^
Line 18: Char 36: note: in instantiation of function template specialization 'std::unordered_map<std::__cxx11::basic_string<char>, int, std::hash<std::string>, std::equal_to<std::__cxx11::basic_string<char>>, std::allocator<std::pair<const std::__cxx11::basic_string<char>, int>>>::unordered_map<int>' requested here
        unordered_map<string, int> vis(number_nodes, 0); // 0=>unvisi
                                   ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable_policy.h:76:5: note: candidate template ignored: substitution failure [with _Iterator = int]: no type named 'difference_type' in 'std::iterator_traits<int>'
    __distance_fw(_Iterator __first, _Iterator __last)
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable_policy.h:64:5: note: candidate function template not viable: requires 3 arguments, but 2 were provided
    __distance_fw(_Iterator __first, _Iterator __last,
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable_policy.h:70:5: note: candidate function template not viable: requires 3 arguments, but 2 were provided
    __distance_fw(_Iterator __first, _Iterator __last,
    ^

我试图在谷歌上搜索这个问题,但没有得到任何明确的答案。 我在行上发现了一些东西,当密钥不可散列时,会抛出一些类似于我得到的错误的错误,但在这种情况下,我的密钥是一个字符串并且字符串是可散列的,所以必须有一些其他问题. 有人知道这是什么原因吗?

【问题讨论】:

  • 你认为你调用的是哪个构造函数?

标签: c++ hash unordered-map


【解决方案1】:

vis 的模板参数是&lt;string, int&gt;,但您尝试使用(number_nodes, 0 对其进行初始化,即&lt;int, int&gt;。无论如何,这是初始化地图的不正确方法。除了默认构造函数之外,还有基于范围、复制、移动和初始化列表constructors。类似的东西

std::map<std::string, int> vis(std::string("hello"), 0);

与有效的构造函数不匹配。也许你想要这样的东西:

std::map<std::string, int> vis;  // default empty constructor
// there are many ways to insert items into maps, this is an example
vis.insert(std::make_pair(std::to_string(number_nodes), 0));

另见Why is “using namespace std;” considered bad practice?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 2018-05-13
    • 2018-06-22
    • 2016-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多