定制操作
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <numeric> #include <list> using namespace std; template <typename Sequence> inline ostream& println(Sequence const& seq) { for (auto const& elem : seq) cout << elem << " "; cout << endl; return cout; } inline bool is_shorter(std::string const& lhs, std::string const& rhs) { return lhs.size() < rhs.size(); } void elimdups(vector<string> &vs) { sort(vs.begin(), vs.end()); auto new_end = unique(vs.begin(), vs.end()); vs.erase(new_end, vs.end()); } int main() { vector<", "Hi", "alan", "wang" }; elimdups(v); stable_sort(v.begin(), v.end(), is_shorter); cout << "ex10.11 :\n"; println(v); system("pause"); ; }
输出结果:
Exercise 10.14:
int main() { auto sum = [](int a, int b) {return a + b; }; cout << sum(, ) << endl; ; }
Exercise 10.16
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; void elimdups(vector<string> &vs) { sort(vs.begin(), vs.end()); for (auto c : vs) cout << c << " "; cout << endl; auto new_end = unique(vs.begin(), vs.end()); vs.erase(new_end, vs.end()); for (auto c : vs) cout << c << " "; cout << endl; } void biggies(vector<string> &vs, size_t sz) { elimdups(vs); stable_sort(vs.begin(), vs.end(), [](string const& lhs, string const& rhs) { return lhs.size() < rhs.size(); }); auto wc = find_if(vs.begin(), vs.end(), [sz](string const& s) { return s.size() >= sz; }); for_each(wc, vs.end(), [](const string &s) { cout << s << " "; }); } int main() { vector<string> v{ ","hi~", "alan", "alan", "cp" }; cout << "ex10.16: " << endl; biggies(v, ); cout << endl; system("pause"); ; }
输出结果:
Exercise 10.18、10.19
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; // from ex 10.9 void elimdups(vector<string> &vs) { sort(vs.begin(), vs.end()); auto new_end = unique(vs.begin(), vs.end()); vs.erase(new_end, vs.end()); for (auto it = vs.cbegin(); it != vs.end(); ++it) cout << *it << " "; cout << endl; } // ex10.18 void biggies_partition(vector<string> &vs, size_t sz) { elimdups(vs); auto pivot = partition(vs.begin(), vs.end(), [sz](const string &s) { return s.size() >= sz; } ); for (auto it = vs.cbegin(); it != pivot; ++it) cout << *it << " "; } // ex10.19 void biggies_stable_partition(vector<string> &vs, std::size_t sz) { elimdups(vs); auto pivot = stable_partition(vs.begin(), vs.end(), [sz](const string& s) { return s.size() >= sz; }); for (auto it = vs.cbegin(); it != pivot; ++it) cout << *it << " "; } int main() { // ex10.18 vector<string> v{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" }; cout << "ex10.18: "; vector<string> v1(v); biggies_partition(v1, ); cout << endl; // ex10.19 cout << "ex10.19: "; vector<std::string> v2(v); biggies_stable_partition(v2, ); cout << endl; system("pause"); ; }
输出结果:
参数绑定
Exercise 10.22:
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <functional> using namespace std; using namespace std::placeholders; bool isLesserThanOrEqualTo6(const string &s, string::size_type sz) { return s.size() <= sz; } int main() { vector<string> authors{ "Mooophy", "pezy", "Queequeg90", "shbling", "evan617" }; cout << count_if(authors.cbegin(), authors.cend(), bind(isLesserThanOrEqualTo6, _1, )) << endl; system("pause"); ; }
Exercise 10.24:
#include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; using namespace std::placeholders; auto check_size(string const& str, size_t sz) { return str.size() < sz; } int main() { vector<, , , , , , , }; "); auto result = find_if(vec.begin(), vec.end(), bind(check_size, str, _1)); if (result != vec.end()) cout << *result << endl; else cout << "Not found" << endl; system("pause"); ; }
#include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; using namespace std::placeholders; auto check_size(string const& str, size_t sz) { return str.size() < sz; } int main() { vector<int> vec{ 0, 1, 2, 3, 4, 5, 6, 7 }; string str("123456"); auto result = find_if(vec.begin(), vec.end(), bind(check_size, str, _1)); if (result != vec.end()) cout << *result << endl; else cout << "Not found" << endl; system("pause"); return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; using namespace std::placeholders; auto check_size(string const& str, size_t sz) { return str.size() < sz; } int main() { vector<int> vec{ 0, 1, 2, 3, 4, 5, 6, 7 }; string str("123456"); auto result = find_if(vec.begin(), vec.end(), bind(check_size, str, _1)); if (result != vec.end()) cout << *result << endl; else cout << "Not found" << endl; system("pause"); return 0; }