It seems to be something related to comparators, what does each thing in it mean? First of all, I would like to add, I don't really understand comparators, something I'll look into when I can but, that aside, I see this kind of syntax many times, and even though I have some idea what it means, I would like someone to provide a detailed answer or resource for this.
1.
What is the first []
, I have seen it as [&]
also how is that different?
The next (const int a, const int b)
I suppose are parameters to a unnamed function, which we declared and are using on the fly? and {return to_string(a) + to_string(b) > to_string(b) + to_string(a);}
is the body, how do we know it's return type is bool?
- Second one seems even more confusing to me, it's converting int to string and and accumulating the result in it. What is the use of
move()
here? if we just wrote returns + to_string(i)
wont that be same too? after all the string s is passed by reference? But more importantly it's not a comparator I think? So then what exactly is it?
I understand I have some gaps in my knowledge and won't be able to understand fully but I can't seem to find this on the internet (maybe I don't know the right terms to search) so it would probably be helpful for others too.
Code 1:
sort(nums.begin(), nums.end(), [](const int a, const int b) { return to_string(a) + to_string(b) > to_string(b) + to_string(a);});
Code 2:
return accumulate(nums.begin(), nums.end(), string(""), [](string& s, int i){ return move(s) + to_string(i);}) ;
}