//课后练习10.20 #include<iostream> #include<vector> #include<string> #include<algorithm> #include<numeric> using namespace std; void elimdups(vector<string> & words) { //按字典序排序 sort(words.begin(), words.end()); auto end_unique = unique(words.begin(), words.end()); words.erase(end_unique, words.end()); } void biggies(vector<string> & words, vector<string>::size_type sz) { elimdups(words); //将word按字典序排序,删除重复单词 stable_sort(words.begin(), words.end(), [](const string& s1, const string & s2){return s1.size() < s2.size(); }); auto wc = count_if(words.begin(), words.end(), [sz](const string s){return s.size() >= sz; }); cout << "there are " << wc << " words whose size is larger than 6" << endl; auto iter = words.end() - wc; for_each(iter, words.end(), [](const string & s){cout << s << endl; }); } int main(void)
{vector<string> words;string word;while (cin >> word){words.push_back(word);}biggies(words, 6);return 0;}/*********************************************************************/
//课后练习10.21 #include<iostream> using namespace std; int main(void) { int num = 20; auto result = [& num]()->bool{ if (num>0){num--;return false;} else return true; }; while (!result()) { cout << num << endl; } cout << result() << endl; }
C++ Primer课后练习10.20,10.21
最新推荐文章于 2024-09-13 21:36:15 发布