C++primer 10.3.4节练习
练习10.22
1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <list> 8 #include <functional> 9 using namespace std; 10 using namespace placeholders; 11 12 void elmDups(vector<string> &vec); 13 14 bool isShorter(string str, string str1); 15 16 void biggies(vector<string> &words, vector<string>::size_type sz); 17 18 bool longThan(const string &s, vector<string>::size_type sz); 19 20 ostream &print(ostream &os, const string &s); 21 22 int main() 23 { 24 vector<string> words{ "the", "quick", "red", "fox", "jumps", "over","the", "slow","red","turtle" }; 25 elmDups(words); 26 stable_sort(words.begin(), words.end(), isShorter); 27 int c = count_if(words.begin(), words.end(), bind(longThan, _1, 5)); 28 cout << c << endl; 29 for_each(words.begin(), words.end(), bind(print, ref(cout), _1)); 30 system("pause"); 31 return 0; 32 } 33 34 void elmDups(vector<string>& vec) 35 { 36 sort(vec.begin(), vec.end()); 37 auto it = unique(vec.begin(), vec.end()); 38 vec.erase(it, vec.end()); 39 } 40 41 bool isShorter(string str, string str1) 42 { 43 return str.size() < str1.size() ? true : false; 44 } 45 46 47 void biggies(vector<string>& words, vector<string>::size_type sz) 48 { 49 elmDups(words); 50 stable_sort(words.begin(), words.end(), isShorter); 51 auto wc = stable_partition(words.begin(), words.end(), [sz](const string &s) {return s.size() < sz;}); 52 auto count = words.end() - wc; 53 cout << count << endl; 54 for_each(wc, words.end(), [](const string &s) { cout << s << " "; }); 55 cout << endl; 56 } 57 58 bool longThan(const string &s, vector<string>::size_type sz) 59 { 60 if (s.size() <= sz) 61 return true; 62 else 63 return false; 64 } 65 66 ostream & print(ostream & os, const string & s) 67 { 68 os << s << " "; 69 return os; 70 // TODO: 在此处插入 return 语句 71 }
练习10.23
bind接受的参数为绑定的函数参数加一,那个一为绑定的函数名;当使用bind生成一个新的可调用的对象时,该对象的参数取决于bind中有几个占位符;
练习10.24
1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <list> 8 #include <functional> 9 using namespace std; 10 using namespace placeholders; 11 12 bool check_size(int i, const string &s2); 13 14 int main() 15 { 16 //vector<string> words{ "the", "quick", "red", "fox", "jumps", "over","the", "slow","red","turtle" }; 17 vector<int> vec{ 1,2,3,4,5,7,8,9,10,11,12, 1 }; 18 auto it = find_if(vec.begin(), vec.end(), bind(check_size, _1, "hello")); 19 cout << *it << endl; 20 system("pause"); 21 return 0; 22 } 23 24 bool check_size(int i, const string & s2) 25 { 26 return i > s2.size(); 27 }
练习10.25
1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <list> 8 #include <functional> 9 using namespace std; 10 using namespace placeholders; 11 12 void elmDups(vector<string> &vec); 13 14 bool isShorter(string str, string str1); 15 16 void biggies(vector<string> &words, vector<string>::size_type sz); 17 18 bool check_size(const string &s1, vector<string>::size_type sz); 19 20 ostream &print(ostream &os, const string &s); 21 22 int main() 23 { 24 vector<string> words{ "the", "quick", "red", "fox", "jumps", "over","the", "slow","red","turtle" }; 25 biggies(words, 5); 26 system("pause"); 27 return 0; 28 } 29 30 void elmDups(vector<string>& vec) 31 { 32 sort(vec.begin(), vec.end()); 33 auto it = unique(vec.begin(), vec.end()); 34 vec.erase(it, vec.end()); 35 } 36 37 bool isShorter(string str, string str1) 38 { 39 return str.size() < str1.size() ? true : false; 40 } 41 42 43 void biggies(vector<string>& words, vector<string>::size_type sz) 44 { 45 elmDups(words); 46 stable_sort(words.begin(), words.end(), isShorter); 47 auto wc = partition(words.begin(), words.end(), bind(check_size, _1, sz)); 48 auto count = words.end() - wc; 49 cout << count << endl; 50 for_each(wc, words.end(), [](const string &s) { cout << s << " "; }); 51 cout << endl; 52 } 53 54 bool check_size(const string & s1, vector<string>::size_type sz) 55 { 56 return s1.size() < sz; 57 } 58 59 ostream & print(ostream & os, const string & s) 60 { 61 os << s << " "; 62 return os; 63 // TODO: 在此处插入 return 语句 64 }
浙公网安备 33010602011771号