《c++ primer》12章习题:文本查询Demo

int main(){
	vector<string>text;
	text.push_back("The Meteorological Agency has called for caution against mudslides and flooding due to a record rainfall in Yamagata Prefecture, northern Japan");
	text.push_back("Weather officials forecast more heavy rain in the Tohoku region and Niigata Prefecture in northern Japan on Tuesday night");
	text.push_back("The officials say that 36 millimeters of rain fell in Uonuma City, Niigata Prefecture, in the one-hour period up to 7 p.m., Tuesday");
	map<string, set<size_t>> query;				//保存每个单词出现过的行号
	map<string, size_t> word_count;				//保存每个单词出现过的次数

	for (size_t st = 0; st < text.size(); ++st)
	{
		string s;
		istringstream iss(text[st]);
		
		while (iss >> s)			//遍历每个单词
		{
			query[s].insert(st);	//存行号和次数
			++word_count[s];
		}
	}
	for (auto i : query)		//打出来试验结果
	{
		cout << i.first <<" ";
		for (auto j : i.second)
		{
			cout << j <<" ";
		}
		cout << endl;
	}
	
	cout << "--------------------" << endl;
	for (auto i : word_count)
	{
		cout << i.first << " occurs" << i.second << " times" << endl;
	}
	cout << "--------------------" << endl;

	string test = "The";
	for (auto i : query[test])
	{
		cout << i << " " << text[i] << endl;
	}
	cout << "'The' occurs " << word_count[test] << " times" << endl;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值