+ 2
how does this code function?
i thought i am returning one or zero and defining operator () to be public not sure regarding why is it being applied on the set as i initialize it , and why it is ordering it when i am expecting it to return true or false https://www.sololearn.com/compiler-playground/cjAAfC2UiRKs
8 Antwoorden
+ 3
here are alternative methods for ordering
https://sololearn.com/compiler-playground/clzfIMpT3dco/?ref=app
here is an article explaining how sorting is happening
https://www.coderslexicon.com/ordering-sets-in-c/
+ 4
std::set accepts a second parameter that can customize the sorting order. in this case, this is your struct. your overloaded operator () was returning lhs>rhs, so it would result in descending order. as you can see in the output...
+ 1
Bob_Li i am not sure where i am getting lost , but my thought process is the following is the intilizer doing insert in the order the element are written and the insert is overloaded by the struct i am using basicllay where is () being applied
+ 1
It is more like i understand that i am asking to sort the set using my struct but failing to understand how all in all the true / false are effecting the locations
+ 1
Okay after doing some research it seems like set is usually implemented like a blanaced binary tree deafult being inserting from smallest to largest
,
So probably 1 and 0 are used to tranverse or right or left ?
Such that for my example 1 2 7 4 5
I first inseat 1 which is the root of the tree ?
Then greater (2,1)
So 2 is to the left of 1
Then not sure what to expect for third element
(7,1) then (7,2)
And so on and then build from the left most child in a recursive manner ?
+ 1
Bob_Li i very much appreciate the time you put into this ,thank you alot ,this was really informative kind of mind blowing that it was ordered by comparing each time we insert a value to go left or right so it happens in o log n and we do not need to check equal since if it is equal then it is overwritten thanks again 🙏
0
Bob_Li this seems like a great article i have to save the site here just reading it until the end i belive this excatly what i am looking for