#include<iostream> #include<fstream> #include<vector> #include<string> #include<algorithm> #include<functional> using namespace std; using namespace std::placeholders; void biggies(vector<string> &words, vector<string>::size_type sz); ostream &print(ostream &os, const string &s, char c); bool check_size(const string & s1, vector<string>::size_type sz); void elimDups(vector<string> &words); bool isShorter(const string &s1, const string &s2); string make_plural(vector<string>::size_type cnt, const string &s1, const string &s2); int main() { ifstream in("test.txt"); if (!in) { cout << "无法打开此文件" << endl; exit(1); } string word; vector<string> vec; while (in >> word) vec.push_back(word); for (const auto &s : vec) cout << s << " "; cout << endl; biggies(vec, 4); } void biggies(vector<string> &words, vector<string>::size_type sz) { elimDups(words); stable_sort(words.begin(), words.end(), bind(isShorter, _1, _2)); auto wc = find_if(words.begin(), words.end(), bind(check_size, _1, sz)); auto count = words.end() - wc; cout << count << " " << make_plural(count, "word", "s") << " length of " << sz << " or longer"; for_each(wc, words.end(), bind(print, ref(cout), _1, ‘ ‘)); } string make_plural(vector<string>::size_type cnt, const string &s1, const string &s2) { if (cnt > 1) return s1 + s2; else return s1; } ostream &print(ostream &os, const string &s, char c) { return os << s << c; } bool check_size(const string & s1, vector<string>::size_type sz) { return s1.size() > sz; } void elimDups(vector<string> &words) { sort(words.begin(), words.end()); auto unique_end = unique(words.begin(), words.end()); words.erase(unique_end, words.end()); } bool isShorter(const string &s1, const string &s2) { return s1.size() < s2.size(); }
#include<iostream> #include<fstream> #include<vector> #include<string> #include<algorithm> #include<functional> using namespace std; using namespace std::placeholders; void biggies(vector<string> &words, vector<string>::size_type sz); ostream &print(ostream &os, const string &s, char c); bool check_size(const string & s1, vector<string>::size_type sz); void elimDups(vector<string> &words); bool isShorter(const string &s1, const string &s2); string make_plural(vector<string>::size_type cnt, const string &s1, const string &s2); int main() { ifstream in("test.txt"); if (!in) { cout << "无法打开此文件" << endl; exit(1); } string word; vector<string> vec; while (in >> word) vec.push_back(word); for (const auto &s : vec) cout << s << " "; cout << endl; biggies(vec, 4); } void biggies(vector<string> &words, vector<string>::size_type sz) { elimDups(words); stable_sort(words.begin(), words.end(), bind(isShorter, _1, _2)); auto wc = find_if(words.begin(), words.end(), bind(check_size, _1, sz)); auto count = words.end() - wc; cout << count << " " << make_plural(count, "word", "s") << " length of " << sz << " or longer"; for_each(wc, words.end(), bind(print, ref(cout), _1, ‘ ‘)); } string make_plural(vector<string>::size_type cnt, const string &s1, const string &s2) { if (cnt > 1) return s1 + s2; else return s1; } ostream &print(ostream &os, const string &s, char c) { return os << s << c; } bool check_size(const string & s1, vector<string>::size_type sz) { return s1.size() > sz; } void elimDups(vector<string> &words) { sort(words.begin(), words.end()); auto unique_end = unique(words.begin(), words.end()); words.erase(unique_end, words.end()); } bool isShorter(const string &s1, const string &s2) { return s1.size() < s2.size(); }