C++primer 10.3.2節練習

來源:互聯網
上載者:User

標籤:tab   lambda運算式   sys   運算式   --   pause   int   turn   for_each   

練習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節練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.