【发布时间】:2015-07-23 18:57:53
【问题描述】:
谁能解释一下auto update_width = [&longest](string const& curr){}
我对函数声明的方式感到困惑,也对函数如何在文件中迭代数据感到困惑。文件有一串单词,程序找到与“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;
【问题讨论】:
-
它是一个lambda 函数。
-
为什么投反对票?如果我不了解 lambdas,我会看到这样的声明,即使使用捕获列表,我肯定也会感到困惑