I am using a multiset to hold Building instances, and I want to order by building height (desc order), and using a lambda express to define it. This part seems ok.
My confusion is about this line, buildings.erase(delta);
, after this line is executed, a building is removed from multiset, my expected behavior is only both id
and height
are equal, the building z
is removed. Wondering why this line buildings.erase(delta);
will remove the building z
in my example?
And is it possible to define a multiset
ordered by height
, but remove only when both id
and height
are equal?
#include <iostream>
#include <set>
using namespace std;
struct Building {
int id;
int height;
Building(int i, int h) {
id = i;
height = h;
}
};
int main() {
auto f = [](Building a, Building b) { return a.height > b.height; };
multiset<Building, decltype(f)> buildings(f);
Building x(1,100);
Building y(2, 200);
Building z(3, 300);
buildings.insert(x);
buildings.insert(y);
buildings.insert(z);
cout << buildings.size() << endl;
Building delta(4, 300);
buildings.erase(delta);
cout << buildings.size() << endl;
}