lambda表达式是C++ 11的新特性,其中一个非常重要特性是传递函数,这个函数可以参考注释,有一个字符串vector,利用很多algorithm里面的算法进行计算。
void biggies(vector<string> &words, vector<string>::size_type sz) { elimDups(words); // put words in alphabetical order and remove dupplicates // sort words by size, but maintain alphabetical order for words of the same size stable_sort(words.begin(), words.end(), [](const string &a, const string &b) {return a.size() < b.size();}); // get an iterator to the first element whose size() is >= sz auto wc = find_if(words.begin(), words.end(), [sz](const string &a) {return a.size() >= sz;}); // compute the number of elements with size >= sz auto count = words.end() - wc; cout << count << " " << make_plural(count, "word", "s") << " of length " << sz << " or longer" << endl; // print words of the given size or longer, each one followed by a space for_each(wc, words.end(), [] const string &s){cout << s << " ";}); cout << endl; }