将向量的值插入无序映射

inserting values of vector into unordered map

我正在尝试将向量中存在的值插入 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));

由于您不想修改矢量,可以将其作为 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<int, int> 中的 vector<int> 的元素数,因此我已经包含了之前的大小检查。

  1. What kind of values does make_pair take? Shouldn't 'n' and '*it' just be simple numerical values that make_pair should accept?

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&&。 如果我们按如下方式重写这一行,这个错误很容易消除:

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

但避免此错误的最简单方法是像这样删除显式模板参数:

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