0

Can anyone explain the auto update_width = [&longest](string const& curr){} I was confused the way function declared and confused how function iterating data inside the file. File has a string of words and program finds the longest words which has most match of "aceimnorsuvwxz".

ifstream ifs("../data/letter.txt");
if (!ifs) return -1;

string longest;
auto update_with = [&longest](string const& curr)
{
    if (string::npos == curr.find_first_not_of("aceimnorsuvwxz"))
        longest = longest.size() < curr.size() ? curr : longest;
};
for (string curr; ifs >> curr; update_with(curr));
cout << longest << endl;

return 0;
yizzlez
  • 8,757
  • 4
  • 29
  • 44
  • Its a [lambda](http://en.cppreference.com/w/cpp/language/lambda) function. – yizzlez Jul 23 '15 at 18:58
  • why the downvotes tho? If I had no knowledge on lambdas, and I would see such declaration, even using capture list, I would for sure be confused too – Creris Jul 23 '15 at 19:04

1 Answers1

0
string longest;    
auto update_with = [&longest](string const& curr)

Is a lambda function capturing 'longest' by reference and a const string ref as parameter and returns a string. update_with is in other words a function now. You can read more about lambda here: Reference to lambda