Using Cmp
I'm sorting the multiset
by a second value of a pair.
Problem 1: I did not used set
because I can't store both {5,3} and {6,3} and I don't know why since they are different.
using pair_type = std::pair<int, int>;
struct Cmp
{
bool operator()(const pair_type& v1, const pair_type& v2) const
{
return v1.second < v2.second;
}
};
int main()
{
multiset<pair_type,Cmp> m;
m.insert({1,1});
m.insert({2,1});
m.insert({3,2});
m.insert({4,2});
m.insert({5,3});
m.insert({6,3});
auto itr = m.find({6,3});
m.erase(itr);
}
Problem 2: Also m.erase
erases {5,3} instead of {6,3} i.e first pair with same second value is erased. In fact m.find({6,3})
returns {5,3}. What causes this and how to solve these two problems?