Explanation of operator < overload function and how to solve problem

PHOTO EMBED

Thu Apr 15 2021 17:02:27 GMT+0000 (Coordinated Universal Time)

Saved by @wowza12341 #c++

bool operator<(const Person &other) const {
 if (name == other.name) { 
    return age < other.age;
 } else { 
        return name < other.name; 
    }
} 
 
//This fixes the problem: In template: invalid operands to binary     expression ('const Person' and 'const Person')
content_copyCOPY

Problem: In template: invalid operands to binary expression ('const Person' and 'const Person') To fix the problem you must have the following code on top. Explanation: The map stores its elements in the order of their keys. Usually this is done by storing the elements in a binary tree structure. When a new element is added to the map, it needs to be inserted in the correct place to maintain the order of the elements. This is done by calling the < operator of the key. The element being inserted is compared to an element in the map. If the return value is true, the element will be inserted before the other element, otherwise it will be inserted afterwards. This continues until the new element is in exactly the right place, between an element which is smaller than it and an element which is bigger than it. So, the short answer is that the operator < is called internally when the element is added and the return value goes into the map infrastructure which then uses it to insert the element in the correct place to maintain the ordering of the elements. The reason why there was a problem is that the map tried to call the < operator for the element but it wasn't defined.

https://www.udemy.com/course/learn-advanced-c-programming/learn/lecture/3688272#questions/14617278