C++Primer课后题10.14、10.20、11.7

来源:互联网 发布:造价软件破解版 编辑:程序博客网 时间:2024/06/18 11:41
//习题10.14void output_words(vector<string> &words){    for (auto i : words) {        cout << i << " ";    }    cout << endl;}void elimDups(vector<string> &words){    sort(words.begin(), words.end());    auto end_unique = unique(words.begin(), words.end());    words.erase(end_unique, words.end());   }void biggies(vector<string> &words, vector<string>::size_type sz){    elimDups(words);    stable_sort(words.begin(), words.end(), [](const string a, const string b) { return a.size() < b.size(); });    auto wc = find_if(words.begin(), words.end(), [sz](const string &a) { return a.size() >= sz; });    auto count = words.end() - wc;    cout << count << " "     << make_plural(count, "word", "s") << " of length "     << sz << " or longer" << endl;    for_each(wc, words.end(), [](const string & s) {cout << s << " "; });    cout << endl;}int main(){    vector<string> words;    string word;    ifstream in("data.txt");    while (in >> word)        words.push_back(word);    biggies(words, 4);    return 0;}//---------------------------------------------------//习题10.20int main(){    int sz = 0;    ifstream in("data.txt");    if (!in) {        cout << "打开失败" << endl;        return -1;    }    vector<string> words;    string word;    while (in >> word)        words.push_back(word);    cout << "文本内容:";    for (auto j : words)        cout << j << " ";    cout << endl;    while (1)    {        cout << "想要知道单词大于几的个数?" << endl;        cin >> sz;        auto i = count_if(words.begin(), words.end(), [sz](const string &str) {return str.size() > sz; });        cout << "大于" << sz << "的单词个数为:" << i << endl;    }}//-------------------------------------------------bool check_size(const string &str1, string::size_type sz){    return str1.size() >= sz;}int main(){    int sz = 5;    ifstream in("data.txt");    if (!in) {        cout << "打开失败" << endl;        return -1;    }    vector<string> words;    string word;    while (in >> word)        words.push_back(word);    cout << "文本内容:";    for (auto j : words)        cout << j << " ";    cout << endl;    auto bc = count_if(words.begin(), words.end(), bind(check_size, _1, sz));    cout << bc << endl; }//--------------------------------------//习题11.7void add_family(map<string, vector<pair<string, string>>> &families, const string &family ){    if (families.find(family) != families.end())        cout << "已经有该家庭,不能再添加" << endl;    else        families[family];}void add_child(map<string, vector<pair<string, string>>> &families, const string &family, const string &child, const string &birthday){    families[family].push_back({ child, birthday });}int main(){    map<string, vector<pair<string, string>>> families;    add_family(families, "张");    add_family(families, "李");    add_family(families, "张");    add_child(families, "张", "学良", "1923-1-2");    add_child(families, "张", "学铭", "1924-2-4");    add_child(families, "赵", "学思", "1929-2-19");    for (auto f : families) {        cout << f.first << "家的孩子:";        for (auto c : f.second)            cout << c.first << "(生日" << c.second << "),";        cout << endl;    }}
0 0