首页 > 代码库 > C++primer 10.3.2节练习

C++primer 10.3.2节练习

练习10.14

1 [] (const int &a, const int &b) {return a + b;}

练习10.15

1 [a] (const int &b) { return a + b; }

练习10.16

 1 void biggies(vector<string>& words, vector<string>::size_type sz)
 2 {
 3     elmDups(words);
 4     stable_sort(words.begin(), words.end(), isShorter);
 5     auto wc = find_if(words.begin(), words.end(), [sz](const string &s) { return s.size() >= sz; });
 6     auto count = words.end() - wc;
 7     cout << count << endl;
 8     for_each(wc, words.end(), [](const string &s) { cout << s << " "; });
 9     cout << endl;
10 }

练习10.17

 1 int main()                                                                            //main函数的部分需要改变
 2 {
 3     vector<Sales_data> Data;
 4     Sales_data data;
 5     int i = 3;
 6     while (i-- != 0)
 7     {
 8         read(cin, data);
 9         Data.push_back(data);
10     }
11     sort(Data.begin(), Data.end(), [](const Sales_data &d1, const Sales_data &d2) { return d1.isbn() < d2.isbn(); });
12     for (auto c : Data)
13     {
14         print(cout, c);
15         cout << endl;
16     }
17     system("pause");
18     return 0;
19 }

对照10.12程序看

练习10.18

 1 void biggies(vector<string>& words, vector<string>::size_type sz)
 2 {
 3     elmDups(words);
 4     stable_sort(words.begin(), words.end(), isShorter);
 5     auto wc = partition(words.begin(), words.end(), [sz](const string &s) {return s.size() < sz;});  //这里将lambda表达式函数体内部改为小于,因为此算法返回的值为指向最后一个为true的元素后一个位置
 6     auto count = words.end() - wc;
 7     cout << count << endl;
 8     for_each(wc, words.end(), [](const string &s) { cout << s << " "; });
 9     cout << endl;
10 }

练习10.19

 1 void biggies(vector<string>& words, vector<string>::size_type sz)
 2 {
 3     elmDups(words);
 4     stable_sort(words.begin(), words.end(), isShorter);
 5     auto wc = stable_partition(words.begin(), words.end(), [sz](const string &s) {return s.size() < sz;});  //这里将lambda表达式函数体内部改为小于,因为此算法返回的值为指向最后一个为true的元素后一个位置
 6     auto count = words.end() - wc;
 7     cout << count << endl;
 8     for_each(wc, words.end(), [](const string &s) { cout << s << " "; });
 9     cout << endl;
10 }

 

C++primer 10.3.2节练习