2018.10.22——10.3定製操作
阿新 • • 發佈:2018-12-16
10.20
#include <iostream> #include <fstream> #include <vector> #include <string> #include <algorithm> using namespace std; bool fuc(string &s) { for (auto iter = s.begin(); iter != s.end() ; ++iter) { if (iter->size() > 6) //報錯 我佛了 不懂 return true; else return false; } } int main(int argc, char *argv[]) { string word; vector<string> s; // ifstream in(argv[1]); // if (!in) // cerr<< "無法開啟檔案" <<endl; while (cin >> word) s.push_back(word); auto it = count_if(s.begin(), s.end(), fuc); cout << it << endl; return 0; }
答案
#include <iostream> #include <fstream> #include <vector> #include <string> #include <algorithm> #include "make_plural.h" using namespace std; inline void output_words(vector<string> &words) { for (auto iter = words.begin(); iter != words.end(); ++iter) cout << *iter << " "; vout << endl; } void biggies(vector<string> &words, vector<string>::size_type sz) { output_words(words); auto bc = count_if(words.begin(), words.end(), [sz] (const stirng &a) {return a.size() >= sz;}); cout << bc<< " " << make_plural(bc, "word", "s") << "of length" << sz << "or longer" << endl; } int main(int argc, char *argv[]) { string word; vector<string> s; ifstream in(argv[1]); if (!in) { cerr<< "無法開啟檔案" <<endl; exit(1); } vector<string> words; string word; while (in>>word) words.push_back(word); biggies(words, 6); return 0; }
10.21
[i] ()mutable ->bool {if(i != 0) {--i; return false:}else return true;};
答案