【问题标题】:inserting values of vector into unordered map将向量的值插入无序映射
【发布时间】:2018-12-15 10:03:28
【问题描述】:

我正在尝试将向量中存在的值插入到 unordered_map 中。我将向量传递给另一个函数,并为向量声明了一个 unordered_map 和一个迭代器。但是在编译时会出现错误(如下)。我想了解为什么会失败。在线搜索让我大致了解了可能出现的问题,但我不清楚:
1. 当我传递不带“&”的向量时,会将向量的副本发送给函数。这到底是什么意思?这在内部如何运作?
2. make_pair 取什么值? 'n' 和 '*it' 不应该只是 make_pair 应该接受的简单数值吗?

#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
using namespace std;

void readValues(vector<int>&v, int n)
{
    int temp;
    while(n--)
    {
        cin>>temp;
        v.push_back(temp);
    }
}

unordered_map<int, int> storeinhashmap(vector<int>v, int n)
{
    vector<int>::iterator it=v.begin();
    unordered_map<int,int>h;
    int temp;
    while(n--)
    {
        temp = *it;
        //cout<<"iter "<<*it<<" "<<++n<<endl;
        h.insert(make_pair<int,int>(n, *it));
        it++;
    }
    return h;
}



int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n, x;
        cin>>n;
        vector<int>v;
        readValues(v, n);
        cin>>x;
        unordered_map<int, int>h = storeinhashmap(v, n);
        //char ans = checksumisx(h, n);

    }
    return 0;
}

错误 -

harshit@harshit-5570:~/Desktop/geeksforgeeks$ g++ -std=c++14 key_pair.cpp 
key_pair.cpp: In function ‘std::unordered_map<int, int> storeinhashmap(std::vector<int>, int)’:
key_pair.cpp:26:43: error: no matching function for call to ‘make_pair(int&, int&)’
         h.insert(make_pair<int,int>(n, *it));
                                           ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from key_pair.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:276:5: note: candidate: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
     make_pair(_T1&& __x, _T2&& __y)
     ^
/usr/include/c++/5/bits/stl_pair.h:276:5: note:   template argument deduction/substitution failed:
key_pair.cpp:26:43: note:   cannot convert ‘n’ (type ‘int’) to type ‘int&&’
         h.insert(make_pair<int,int>(n, *it));

【问题讨论】:

  • 不完全确定为什么(留给专家),但将make_pair&lt;int,int&gt;(n, *it) 更改为make_pair(n, *it)pair&lt;int,int&gt;(n, *it) 对我有用。 make_pair 的重点是您不必明确说明类型。
  • 并在循环中声明 temp 并在您有疑问时使用它。
  • @MatthieuBrucher 你能告诉我为什么需要这样做吗?

标签: c++ vector unordered-map


【解决方案1】:
  1. make_pair 采用什么样的值? 'n' 和 '*it' 不应该只是 make_pair 应该接受的简单数值吗?

std::make_pair 声明如下(例如 n3337 中的 20.3.3):

template <class T1, class T2>
  pair<V1, V2> make_pair(T1&& x, T2&& y);

因此,如果我们像您一样显式设置这些模板参数,则不会发生类型推导,并且此函数会产生

pair<int, int> make_pair(int&& x, int&& y);

然后

h.insert(make_pair<int,int>(n, *it));

显示编译错误,因为n*it 都是左值,而不是int&amp;&amp;。 如果我们将这一行改写如下,这个错误很容易消除:

h.insert(make_pair<int,int>(std::move(n), std::move(*it)));

但避免此错误的最简单方法是删除显式模板参数,如下所示:

h.insert(make_pair(n, *it));

【讨论】:

    【解决方案2】:

    由于您不想修改向量,您可以将其作为const 引用参数传递以避免无用的副本:

    unordered_map<int, int> storeinhashmap(const vector<int>& v, int n)
    {
        // Check that the number of elements to be inserted
        // is less than the size of vector
        if (n < 0 || n > v.size()) {
            throw invalid_argument("Wrong number of vector elements to be inserted.");
        }
    
        unordered_map<int,int>h;
        for (size_t i = 0; i < (size_t)n; i++) {
            h.insert(make_pair(n-i, v[i]));
        }
        return h;
    }
    

    此外,我知道n 是要插入unordered_map&lt;int, int&gt; 中的vector&lt;int&gt; 的元素数,因此我已经包含了之前的大小检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多