標籤: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節練習