11.2.2节练习

练习11.9 定义一个map,将单词与一个行号的list关联,list中保存的是单词所出现的行号。

#include <iostream>
#include <map>
#include <list>
#include <string>
#include <fstream>
using namespace std;
int main()
{
	map<string, list<size_t>> word_lines;
	ifstream in("11.9.txt");
	string word;
	size_t line = 1;
	//这里只考虑一行只有一个单词的情况。
	while (getline(in, word)) {
		word_lines[word].push_back(line);
		++line;
	}

	for (auto i : word_lines) {
		cout << i.first << " occurs on: ";
		for (auto l : i.second) {
			cout << l << " ";
		}
		cout << "line" << endl;
	}
	return 0;
}

练习11.10 可以定义一个vector<int>::iterator到Int的map吗?list<int>::iterator到int的map呢?对于两种轻重,如果不能,解释为什么?

 = 。=  两个如下的定义都通过了。

#include <iostream>
#include <vector>
#include <map>
#include <list>
using namespace std;
int main()
{
	map<vector<int>::iterator, int> s1;
	map<list<int>::iterator, int> s2;
}

但是!第二个是会出错的。(P378)在实际编程中,重要的是,如果一个类型定义了“行为正常”的<运算符,则它可以用作为关键字类型。

然而list迭代器类型并不支持<运算符。


练习11.11 不使用decltype重新定义bookstore。

multiset<Sales_data, bool (*)(const Sales_data &, const Sales_data &)>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值