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

//习题10.14
void 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.20

int 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.7
void 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值