0

I was browsing through code and came across an interesting way of writing a return statement and I am wondering if someone could help me understand what this code is doing and what it would look like in a different format. the code is this:

bool Maze::EqualityCheck(std::string & str_1, std::string & str_2) {
    return (
    (str_1.size() == str_2.size()) && equal(str_1.begin(), str_1.end(), str_2.begin(), [](char & c1, char & c2) {
      return (c1 == c2 || toupper(c1) == toupper(c2));
    })
  );
}

I have never seen anything like this before and I am hoping to gain insight on this novelty (to me) thank you.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Tristan
  • 9
  • 4
  • 1
    Which part don't you understand? lambda? – Jarod42 Feb 09 '21 at 16:38
  • @Jarod42 I don't see the lambda? I don't understand some of the [] brackets towards the end of line 3. how come there are two returns? how else could this be written? or is this the only way to write this type of code? – Tristan Feb 09 '21 at 16:40
  • 1
    `[](char & c1, char & c2) { return (c1 == c2 || toupper(c1) == toupper(c2)); }` is a [lambda](https://en.cppreference.com/w/cpp/language/lambda). – Jarod42 Feb 09 '21 at 16:44
  • @Jarod42 so what does the lambda do? – Tristan Feb 09 '21 at 16:46
  • 2
    It is a local functor. follow link for more details. – Jarod42 Feb 09 '21 at 16:47

0 Answers0