0

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?

  1. 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 return s + 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);}) ;
 }
Xgh05t
  • 210
  • 2
  • 10
  • "I don't really understand comparators, something I'll look into when I can" now is the time to do that. Actually it is rather simple, comparators compare objects, thats basically it. Please one question per question – 463035818_is_not_a_number Jan 07 '20 at 12:52
  • 1
    the duplicate is for your first question – 463035818_is_not_a_number Jan 07 '20 at 12:54
  • 1
    Hard to search for something when you don't know what it is called. **Lambda expression** is the phrase you are looking for. – Eljay Jan 07 '20 at 12:56
  • I understand that they compare the thing that confuses me is how they are used or implemented to arrange things how you like and thank you for the reference, it will help with the syntax part of things, but one question still remains, what is that third parameter supposed to be in the accumulate function? – Xgh05t Jan 07 '20 at 13:09
  • 1
    The [ ] are the captures. You can use local variables within a lambda function, and you can do so, for instance, by reference instead of copy. Example: you have a matrix class that can produce a lambda function which is a linear operator defined by the matrix that uses a reference to the matrix data in order not having to copy a possibly large matrix. Search for something like "c++ lambda closure tutorial", https://www.cprogramming.com/c++11/c++11-lambda-closures.html seems to be quite informative. – Aziuth Jan 07 '20 at 13:15
  • 1
    As for accumulate, you might want to read https://en.cppreference.com/w/cpp/algorithm/accumulate . Has some code which might help you. In short: this is the function which is used to integrate an entry in an existing "block". The `move` does some clever stuff in order to save memory and avoid unnecessary copy actions, maybe ignore it in order to understand what the code basically does and only when you do go for it's meaning. – Aziuth Jan 07 '20 at 13:25
  • 1
    The third parameter of accumulate is the starting value. The fourth parameter is how to "add" an element to the current value. That whole line "adds" numbers as a long string of all the digits, e.g. if `nums` is `{ 1, 2, 3, 45, 6, 78 }` you get "12345678" as a result. – Caleth Jan 07 '20 at 14:01
  • 1
    The standard calls it a *BinaryOperation* a.k.a. "function of two parameters". I know that isn't a helpful name, but it can be a function that does *almost anything*. – Caleth Jan 07 '20 at 14:19
  • I understand what it does completely now, thank you for very helpful answers, just one little thing I still don't understand is how those binary and compare function take inputs, what I mean is in the accumulate, I presume we're returning the ```move(s) + to_string(i)``` to the empty string in parameter 3, but how is it taking (or mapping) those values to ```string& s, int i```, I understand the int is coming from the ```nums``` vector and string from that string ```string("")``` but how?, similarly in sort, how are the ```int a``` and ```int b``` selected? I will try to follow along debugger – Xgh05t Jan 07 '20 at 15:31
  • But like a conceptual explanation of it, in case someone understands what I said – Xgh05t Jan 07 '20 at 15:32
  • After following the debugger and reading about accumulate, the conclusion I have reached is comparators, morph the inputs into the form you like and compare these new morphed forms and give result accordingly and the binary operator in accumulate is in fact taking the empty string and value from range as 1st and 2nd params, and that what confuses me really is not binary operations, but their different use in different algorithms and containers, one step at a time – Xgh05t Jan 08 '20 at 01:10

0 Answers0