C++ Primer课后练习11.20,11.23,11.26,11.27,11.28,11.29,11.30,11.31,11.32

本文通过多个实例展示了如何使用C++ STL中的map和multimap容器进行数据管理和操作,包括插入、查找、删除等基本操作及一些高级用法。
/*练习11.20*/
#include
#include
#include
#include
using namespace std;
int main(void)
{
	map maper,maper1;
	string word,word1;
	while (cin >> word)
	{
		auto ret = maper.insert(make_pair(word, 1));
		if (!ret.second)
			ret.first->second++;
		++maper1.insert({ word, 0 }).first->second;
	}
	
	for (auto ss : maper)
	{
		cout << ss.first << " occurs " << ss.second << " times" << endl;
	}
	for (auto ss : maper1)
	{
		cout << ss.first << " occurs " << ss.second << " times" << endl;
	}
}
/***********************************************************************************************/
/*练习11.23*/
#include
#include
#include
#include
#include
using namespace std;
int main(void)
{
	multimap<string, vector> family;
	string familyname, offspringname;
	vector fuck;
	while ([&]()->bool{
		cout << "please enter the family name!" << endl;
		return cin >> familyname && familyname != "AA"; }())
	{
		cout << "please enter the offspring's name" << endl;
		while (cin >> offspringname && offspringname != "AA")
		{
			
			fuck.push_back(offspringname);
		}
		family.insert( make_pair(familyname, fuck ));
	}
		for (auto ss : family)
		{
			cout << ss.first << ": ";
			for (auto ss1 : ss.second)
				cout << ss1 << " ";
			cout << endl;
		}


}
/****************************************************************************************************/
//练习11.26
#include
#include
#include
#include
using namespace std;
int main(void)
{
	map m;
	m[0] = 1;
	for (auto ss : m)
		cout << ss.first << " " << ss.second << endl;
	vector v;
	//v[0] = 1;
	/*for (auto ss : v)
		cout << ss << endl;*/

	map mapp = { { 1, "hehe" }, { 2, "haha" } };
	map::key_type type_to_subscript = 1;
	map::mapped_type type_to_return = mapp[type_to_subscript];
	cout << type_to_return << endl;
}
/*******************************************************************************************/
#include
#include
#include
#include
#include
using namespace std;
/*
11.27
对于关键字不唯一,例如multimap和multiset使用count,
对于关键字唯一的,我使用find;
*/
/*练习11.28*/
int main(void)
{
	map<string, vector> mapper = { { "king", { 1, 2, 3, 4 } }, { "queen", { 5, 6, 7, 8 } } };
	map<string, vector>::iterator it_mapper = mapper.find("king");
	if (it_mapper != mapper.end())
		cout << "find it!" << endl;
	else
		cout << "fuck" << endl;

}
/*
11.29
如果关键字不在容器中,
则lower_bound,upper_bound会返回相等的迭代器,关键字的第一个安全插入点--不影响容器中元素顺序的插入位置
对于equal range,若未找到匹配元素,则两个迭代器都指向关键字可以插入的位置。

11.30
pos是一个pair,pos.first表示第一个符合个关键字位置的元素,
之后继续累加,pos.first->second则表示书名
*/
/*****************************************************************************************************************/
//练习11.31
#include
#include
#include
#include
using namespace std;
int main(void)
{
	multimap author;
	for (string name, title; cin >> name >> title;)
	{
		author.insert({ name, title });
	}
	auto result = author.find("wuchengen");
	auto number = author.count("wuchengen");
	while (number)
	{
		if (result->second == "xiyouji")
		{
			result=author.erase(result);
			
		}
		else
		{
			++result;
		}
		--number;
	}
	for (auto ss : author)
	{
		cout << ss.first << " " << ss.second << endl;
	}
	return 0;
}
/*****************************************************************************************************************/
//练习11.32
#include
#include
#include
#include
#include
using namespace std;
int main(void)
{
	multimap<string, set> author;
	string name;
	while ([&]()->bool{
		cout << "please input the author's name" << endl;
		return cin >> name && name!="AA";
	}())
	{
		cout << "please input the books" << endl;
		string title;
		set bookname;
		while (cin >> title && title!="AA" )
		{
			bookname.insert(title);
		}
		author.insert({ name, bookname });
	}
	
	for (auto ss : author)
	{
		cout << ss.first << " ";
		for (auto sss : ss.second)
			cout << sss << ", ";
		cout << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值