Search code examples
c++c++11data-structuresunordered-mapmultimap

How can I make an unordered_multimap in C++ between an int and a pair?


When trying to run the following code I get this compiling error "error: type/value mismatch at argument 2 in template parameter list for ‘template class std::unordered_multimap’ unordered_multimap m;"

Is there any way I can setup an multimap? If not how can I do what I want to do? Thanks!

#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <map>
#include <unordered_map>
#include <utility>

using namespace std;

int main(){

int n = 100;
unordered_multimap<int, pair> m; //Error is in this line

for (int a = 0; a <= n; ++a)
    for (int b = 0; b <= n; ++b)
    {
        int result = (a*a*a) + (b*b*b);
        pair<int,int> p = {a,b};
        pair<int,pair> p2 = {result,p};
        m.insert(p2);
    } 
return 0;
}

Solution

  • A std::pair isn't a type by itself, it's a template that "generates" types. You need to specify what type you want to "make" by specifying the 2 template argument types that it asks for.

    Your use case shows that you want to have two ints as a pair so you should specify that everywhere:

    unordered_multimap<int, pair<int, int>> m;
    

    and

    pair<int,pair<int, int>> p2 = {result,p};