When inserted into a set, does the set internally delete several objects several times? I tried to insert two objects of type MyClass, as in the following program, but, to my surprise, it calls the class destructor with the value 2 times originally inserted! I can not understand any logic of this. Can someone give an idea of the exit? (in bold)
#include<stdio.h> #include<stdlib.h> #include<set> using namespace std; struct MyClass { double num; ~MyClass() { printf("Destructor called..for val: %lf\n", num); } }; typedef int (*fun_comp)(MyClass, MyClass); int comp(MyClass a, MyClass b) { return a.num-b.num; } int main() { fun_comp fptr; fptr = ∁ set<MyClass, int (*)(MyClass, MyClass)> b(fptr); for(int i=3; i< 5; i++) { printf("started with i: %d....\n\n", i); { MyClass m; m.num=i*1.134; b.insert(m); printf("Inserted val: %lf\n", m.num); } printf("ended....\n\n"); } printf("Done with insert..\n"); return 0; }
output: started with i: 3 ....
Box val: 3.402000
Destructor called ... for val: 3.402000
over ....
started with i: 4 ....
Destructor called ... for val: 4.536000 <------- why is this freed before insertion
The destructor invoked ... for val: 3.402000 <------- multiple invocation of the destructor for this significant object
Destructor called ... for val: 4.536000 <-------- ??
Destructor called ... for val: 3.402000 <------ again !!
Box val: 4.536000
Destructor called ... for val: 4.536000
over ....
Done with the insert.
Destructor called ... for val: 3.402000
Destructor called ... for val: 4.536000
source share