C++PrimerPlus(第6版)中文版:Chapter16.4.6关联容器_setops.cpp

本文详细介绍了C++标准库中的set和multimap容器的使用方法,包括基本操作、集合运算以及通过示例代码展示如何进行元素的插入、查找和删除等。此外,还解释了set与multimap之间的区别及其应用场景。

本小节主要讲set的使用方法。

关联容器将值和键关联在一起,并使用键来查找值。

关联容器的优点在于:它提供了对元素的快速访问。插入元素的时候,不需要指定插入位置,因为关联容器是排过序的,它知道新元素来了该放到哪里去。

关联容器通常是用某种树来实现的。相对链表而言,树的查找速度更快。

最简单的关联容器就是set ,意思是集合。它的值类型和键是一样的,键是唯一的,也意味着集合中不会有相同的键,或者说值也是唯一的。值就是键。multiset类似于set 只是可能有多个值的键是相同的。

map中值与键的类型不同,键是唯一的,每个键只对应一个值。multimap与map类似,只是一个键可以与多个值相关联。

set也使用模板参数类确定存储的值的类型。

set<string> A //  a set of string objects 

集合的运算主要有:并集、交集、差集、等,具体的请看例子代码。

// Example16.13setops.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <set> 
#include <algorithm>
#include <iterator>
#include <map>//为了演示multimap的用法
//下面的代码是为了演示multimap的用法
typedef int KeyType;
typedef std::pair<const KeyType, std::string> Pair;
typedef std::multimap<KeyType,std::string> MapCode;
//上面的代码是为了演示multimap的用法


int main()
{
   // std::cout << "Hello World!\n";
	using namespace std;
	const int N = 6;
	string s1[N] = {"buffoon","thinkers","for","heavy","can","for"};
	string s2[N] = { "metal","any","food","elegant","deliver","for"};
	set<string> A(s1,s1+N);//数组中for出现了两次,在集合A中只会出现一次
	set<string> B(s2, s2 + N);

	ostream_iterator<string, char> out(cout," ");
	cout << "Set A :";
	copy(A.begin(),A.end(),out);//数组中for出现了两次,在集合A中只会出现一次,而且集合中元素会被排序
	                            //Set A :buffoon can for heavy thinkers
	cout << endl;

	cout << "Set B :";
	copy(B.begin(), B.end(), out);//Set B :any deliver elegant food for metal
	cout << endl;

	cout << "Union of A and B :";
	set_union(A.begin(),A.end(),B.begin(), B.end(), out);//Union of A and B :any buffoon can deliver elegant food for heavy metal thinkers
	cout << endl;

	cout << "Intersection of A and B :";
	set_intersection(A.begin(), A.end(),B.begin(), B.end(), out);// Intersection of A and B :for
	cout << endl;

	cout << "Difference of A and B :";
	set_difference(A.begin(),A.end(),B.begin(), B.end(), out);//两个集合的差是第一个集合减去两个集合都有的元素。Difference of A and B :buffoon can heavy thinkers
	cout << endl;

	set<string> C;
	cout << "Set C:";
	set_union(A.begin(), A.end(), B.begin(), B.end(), insert_iterator<set<string>>(C,C.begin()));
	copy(C.begin(),C.end(),out);//Set C:any buffoon can deliver elegant food for heavy metal thinkers
	cout << endl;

	string s3("grungy");
	C.insert(s3);
	cout << "Set C after insertion:";
	copy(C.begin(),C.end(),out);//Set C after insertion:any buffoon can deliver elegant food for grungy heavy metal thinkers
	cout << endl;

	cout << "Showing a range:";
	
	
	copy(C.lower_bound("ghost"),C.upper_bound("spook"),out);//lower_bound()将键作为参数,并返回一个迭代器,该迭代器指向集合中第一个不小于键参数的成员,本例子是返回 grungy
	                                                       //upper_bound()将键作为参数,并返回一个迭代器,该迭代器指向集合中第一个大于键参数的成员,本例子是返回了 thinkers
	//Showing a range:grungy heavy metal
	cout << endl;
	//下面的代码是为了演示multiset 的用法
	cout << "***下面的代码是为了演示multimap 的用法***\n";
	MapCode codes;
	codes.insert(Pair(415,"San Francisco"));
	codes.insert(Pair(510, "Oakland"));
	codes.insert(Pair(718, "Brooklyn"));
	codes.insert(Pair(718, "Stanten Island"));
	codes.insert(Pair(415, "San Rafael"));
	codes.insert(Pair(510, "Berkeley"));
	cout << "Number of cities with area code 415: "<<codes.count(415)<<endl;
	cout << "Number of cities with area code 718: " << codes.count(718) << endl;
	cout << "Number of cities with area code 510: " << codes.count(510) << endl;
	cout << "Area Code City\n";
	MapCode::iterator it;
	for (it = codes.begin();it != codes.end(); ++it)
		cout << " " << (*it).first << " " << (*it).second << endl;
	pair<MapCode::iterator, MapCode::iterator> range = codes.equal_range(718);
	cout << "Cities with area code 718: \n";
	for (it = range.first; it != range.second; ++it)
		cout << (*it).second << endl;
		//上面的代码是为了演示multiset 的用法
	return 0;
}


以上代码还包括了multimap的使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值