Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No instance of function template "std::make_pair" matches the argument list

Is it legal to use unordered_map for a pair type like <int, vector<vector<int>>>? Visual Studio warns me "No instance of function template "std::make_pair matches the argument list" in line 16 (map.insert(make_pair<int, vector<vector<int>>>(i + m, tt))):

void test(vector<int>& nums) 
{
    unordered_map<int, vector<vector<int>>> map;
    unordered_map<int, unordered_set<int>> map2;
    vector<vector<int>> results;

    for (int i = 0; i < nums.size(); i++)
    {
        for (int m = i + 1; m < nums.size(); m++)
        {
            if (!map.count(i + m))
            {
                vector<int> t{ i, m };
                vector<vector<int>> tt;
                tt.push_back(t);
                map.insert(make_pair<int, vector<vector<int>>>(i + m, tt));
                map2.insert(make_pair<int, unordered_set<int>>(i + m, unordered_set<int>(i - m)));
            }
            else if (map2.at(i + m).count(i - m) || map2.at(i + m).count(m - i))
            {
                continue;
            }
            else
            {
                map.at(i + m).push_back(vector<int>{i, m});
                map2.at(i + m).insert(i - m);
            }
        }
    }

}

like image 837
NJUHOBBY Avatar asked Jan 05 '23 20:01

NJUHOBBY


2 Answers

The problem seems to be std::make_pair(): you should have this template deduce the appropriate types:

map.insert(std::make_pair(i + m, tt));

According to the C++ standard 20.3.3 [pair.spec] paragraph 9 std::make_pair() is declared something like this:

template <typename F, typename S>
std::pair<???, ???> make_pair(F&& first, S&& second)

(with suitable types filled in for the ???). However, the types you specified wouldn't match the arguments, indeed! If you wanted to specify the types correctly, you'd use

std::make_pair<int, std::vector<std::vector<int>>&>(i + m, tt)
like image 80
Dietmar Kühl Avatar answered Jan 07 '23 11:01

Dietmar Kühl


You are not supposed to pass type arguments to make_pair; it is supposed to deduce them, then forward them to a pair of values.

Simply remove the <int, vector<vector<int>>> portion after make_pair.

You can pass types and make it work, but the rules are arcane, and the procedure useless; by passing types, you are doing it wrong.

If you want to pass the types, replace make_pair with pair and construct directly. The point of make_pair is not having to pass the types.

If you want to know more about this particular error and how make pair works, read up about template function type deduction and forwarding references.

like image 20
Yakk - Adam Nevraumont Avatar answered Jan 07 '23 10:01

Yakk - Adam Nevraumont