c++ primer 学习笔记10--关联容器

书本376页。练习11.4:单词计数,忽略大小写,与标点。

#include <iostream>  
#include <vector>
#include <string>
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;

string& trans(string &s)
{
	for (int p = 0; p < s.size(); ++p)
	{
		if (s[p] >= 'A'&& s[p] <= 'Z')
		{
			s[p] -= 'A' - 'a';
		}
		else if (s[p] == ',' || s[p] == '.')
		{
			s.erase(p, 1);
		}

	}
	return s;
}

int main(int argc,char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
	{
		cerr << "打开输入文件失败" << endl;
		return -1;
		//exit(1);
	}
	map<string, size_t> word_count;
	string word;
	while (cin >> word)
	{
		++word_count[word];
	}
	for (auto &w : word_count)
	{
		cout << w.first << "出现了" << w.second << "次" << endl;
	}
}

练习11.7

定义一个map,关键字是家庭的姓,保存的是孩子的名。实现添加新的家庭以及向已有的家庭添加孩子。

#include <iostream>  
#include <vector>
#include <string>
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;
void add_family(map<string, vector<string>> &families, const string &family)
{
	if (families.find(family) != families.end())
	{
		families[family] = vector<string>();
	}
}
void add_child(map<string, vector<string>> &families, const string &family,const string &child)
{
	families[family].push_back(child);
}

int main(int argc,char *argv[])
{
	map<string, vector<string>> families;
	add_family(families, "张");
	add_child(families, "张", "强");
	add_child(families, "张", "刚");
	add_child(families, "王", "五");
	add_family(families, "王");

	for (auto f : families)
	{
		cout << f.first << "家的孩子";
		for (auto c : f.second)
		{
			cout << c << " ";
		}
		cout << endl;
	}
}

11.9,定义一个map,将单词与一个行号的list关联,list保存的是单词所在的行号。

#include <iostream>  
#include <vector>
#include <string>
#include <fstream>
#include <map>
#include <algorithm>
#include <list>
#include <sstream>
using namespace std;

string& trans(string &s)
{
	for (int p = 0; p < s.size(); ++p)
	{
		if (s[p] >= 'A'&& s[p] <= 'Z')
		{
			s[p] -= 'A' - 'a';
		}
		else if (s[p] == ',' || s[p] == '.')
		{
			s.erase(p, 1);
		}

	}
	return s;
}

int main(int argc, char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
	{
		cerr << "打开输入文件失败" << endl;
		return -1;
		//exit(1);
	}

	map<string, list<int>> word_lineno;
	string line;
	string word;
	int lineno = 0;
	while (getline(in, line))
	{
		++lineno;//行号增加
		istringstream l_in(line);//构造字符串流,读取单词
		while (l_in >> word)
		{
			trans(word);
			word_lineno[word].push_back(lineno);
		}
	}
	for (auto &w : word_lineno)
	{
		cout << w.first << "所在行: " ;
		for (auto i : w.second)
		{
			cout << i << " ";
		}
		cout << endl;
	}
}
11.12.编写程序,读入string 和int的序列,将每个string 和int存入一个pair中,pair保存在一vector中

#include <iostream>  
#include <vector>
#include <string>
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;

int main(int argc, char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
	{
		cerr << "打开输入文件失败" << endl;
		return -1;
		//exit(1);
	}
	vector<pair<string, int>> data;
	string s;
	int v;
	while (in >> s && in >> v)
	{
		data.push_back(pair<string, int>(s, v));
		//data.push_back(make_pair(s,v));
		//data.push_back({s,v});
	}
	for (auto &w : data)
	{
		cout << w.first << " " << w.second << endl;
	}
}







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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值