Search code examples
c++std-pairmultiset

How to insert a pair using multiset in C++


I want to insert an integer value and a pair in a multiset.

So I declared it as:

multiset < int, pair < int, int> > mp;
int m,n,p;

To insert in multiset I tried this :

mp.insert(make_pair(m, make_pair(n,p))); // Compile time error

But its giving compile time error... Could someone please suggest the correct method to implement it.


Solution

  • The type multiset<int,pair<int,int>> is trying to create a multiset where the Key is int and the Compare is pair<int,int>. This is nonsensical. You either want to use

    multiset<pair<int,pair<int,int>>>
    

    or you want to use

    multiset<tuple<int,int,int>>
    

    The former type (pair<int,pair<int,int>>) matches the expression you're using to insert into the set (make_pair(m, make_pair(n,p))). If you use the latter, you'll want make_tuple(m,n,p).