11.3.2

11.20

#include <iostream>
#include <map>
#include <string>

using std::string;
using std::map;
using std::cin;
using std::cout;

int main()
{
    map<string, size_t> counts;
    for(string word; cin >> word;)
    {
        auto result = counts.insert({ word, 1 });
        if(!result.second)
            ++result.first->second;
    }
    for(auto const& count : counts)
        cout << count.first << " " << count.second << ((count.second > 1) ? " times\n" : " time\n");
}

11.21

while (cin >> word)
    ++word_count.insert({ word, 0 }).first->second;

对于每一个输入的word
如果能插入进去,则插入{word , 0}。然后递增0 ,变为1
如果关键字已经存在,只递增map的值部分

11.22

std::pair<std::string, std::vector<int>>    // argument
std::pair<std::map<std::string, std::vector<int>>::iterator, bool> // return

11.23

 multimap<string, string> families;
    for (string lname, cname; cin >> cname >> lname; families.emplace(lname, cname));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值