When I run the following code (with a line commented out) I get no output (no comparisons are made). But the moment I uncomment the last line I get three lines of output (three comparisons are made). When a second item is added, 1-2 comparisons is understandable, but why a third?
#include <map>
#include <iostream>
using namespace std;
class MyType
{
public :
int a;
MyType():a(0){}
};
bool operator<(const MyType &lhs, const MyType &rhs)
{
cout<<"operator < is called\n";
return lhs.a < rhs.a;
}
int main()
{
MyType T1,T2;
T1.a = 100;
T2.a = 33;
map<MyType,int> Test;
Test[T1] = 10;
//Test[T2] = 20; --> #Uncomment this line for getting 3 lines of "operator < is called"
}
The output with the line uncommented:
operator < is called
operator < is called
operator < is called
How did it end up in printing three strings from zero with just one extra key got added?
Here are the addresses of lhs and rhs:
0x56365ea45e90
0x7ffe3c4cd47c
operator < is called
0x7ffe3c4cd47c
0x56365ea45e90
operator < is called
0x56365ea462d0
0x56365ea45e90
operator < is called