Harshit Nagar
Harshit Nagar

Reputation: 438

inserting values of vector into unordered map

I am trying to insert the values present in a vector into a unordered_map. I passed the vector to another function and declare an unordered_map and an iterator to the vector. But while compiling it gives error(below). I would like to understand why does this fail. Searching online has given me a rough idea as to what might be wrong but it is not clear to me:
1. When i pass the vector without '&', a copy of the vector is sent to the function. What does this exactly mean? How does this works internally?
2. What kind of values does make_pair take? Shouldn't 'n' and '*it' just be simple numerical values that make_pair should accept?

#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;
}

Error -

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));

Upvotes: 0

Views: 1255

Answers (2)

Hiroki
Hiroki

Reputation: 2880

  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 is declared as follows (e.g. 20.3.3 in n3337):

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

Thus if we explicitly set these template parameters like you, no type deductions occur and this function yields

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

Then

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

shows compilation error because both n and *it are lvalues, not int&&. This error is easily removed if we rewrite this line as follows:

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

But the most simple way to avoid this error is removing explicit template parameters like this:

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

Upvotes: 2

aeroyorch
aeroyorch

Reputation: 56

As you do not want to modify the vector, you can pass it as const reference argument in order to avoid useless copies:

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;
}

In addition, I understood that n is the number of elements of the vector<int> to be inserted within unordered_map<int, int>, hence I have included a previous size check.

Upvotes: 1

Related Questions