I created a set of C-lines, providing my own comparator function, because I wanted it to accept only the first three characters. Here is its definition:
struct set_object { bool operator()(const char* first, const char* second) { return strncmp(first, second, 3) > 0; } }; std::set<const char*, set_object> c_string_set;
It works the way I wanted it, sorting the lines, adding them as I described in the set_object class. But the interesting part starts when I try to add a line that compares with the one already added. For example, if I try to add “aaab” when there is already “aaa” in the set, it does not add it to the set. If I add “aaab” first, then try adding “aaa”, it only displays “aaab”. But how does he know when they are equal, if I provided only a function that returns true when one of the lines is larger? It should return false when it is equal to or less!
To clarify, this is not a problem, just an attempt to figure out how C ++ works.
source share