关联容器(set、multimap)

关联容器

有序关联容器

  • 关联容器是将值和键绑定在一起,并使用键查找值。
  • 提供了对元素的快速访问,和序列相同,
  • 允许插入新元素,但不能指定元素的插入位置
  • 通常是使用某种实现的,相对于链表,树的查找速度更快
  • set、multiset、map、multimap

set

  • 可反转、排序、
  • 经过排序的关联容器(有序的
  • 键是唯一的,所以不能存储多个相同的值
  • 键和值类型相同,值就是键

//===============================================================
//FileName:
//          CPlusPlus.cpp
//Date:
//          2019/11/29
//Author:
//          khoing(https://blog.csdn.net/qq_45391763)
//===============================================================
#include <iostream>
#include <set>
#include <iterator>
#include <algorithm> //for_each , find, count,copy等



int main()
{
	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);
	set<string> B(s2, s2 + N);//将迭代器区间作为参数的构造函数,左闭右开


//------------------------------------------------------------------------
	ostream_iterator<string, char> out(cout, " ");//从string到char,放到cout输出流的为char
	//标准程序库定义有供输入及输出用的iostream iterator类,称为istream_iterator和ostream_iterator,
	//分别支持单一型别的元素读取和写入。使用这两个iteratorclasses之前,先得含入iterator头文件:
	//#include<iterator>
	//ostream_iterator在STL中一般配合copy函数一起使用
	cout << "Set A: ";
	copy(A.begin(), A.end(), out);//把向量A中的数据放到cout输出流中,通过流迭代器out.
	cout << endl;

//------------------------------------------------------------------------
	cout << "Set B: ";
	copy(B.begin(), B.end(), out);
	cout << endl;

//------------------------------------------------------------------------
	cout << "Union of A and B:\n";
	set_union(A.begin(), A.end(), B.begin(), B.end(), out);
	//合并A和B,并集包含两个集合合并后的内容。如果A、B中包含相同的值,则这个值将在并集中只出现一次,因为键是唯一的。
	cout << endl;

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


//------------------------------------------------------------------------
	cout << "Difference of A and B:\n";
	set_difference(A.begin(), A.end(), B.begin(), B.end(), out);
	cout << endl;

//------------------------------------------------------------------------
	set<string> C;
	cout << "Set C:\n";
	set_union(A.begin(), A.end(), B.begin(), B.end(),insert_iterator<set<string> >(C, C.begin()));
	//将结果放到集合C中,最后一个结果不能是C.begin,
		//因为set每个键都是常量,c.begin返回的迭代器是常量迭代器,不能用作输出迭代器,
		//与copy相似,set_union将覆盖容器中已有的数据,并要求容器有足够的控件容纳信息,c是空的,不满足这些
	//insert_iterator可以将复制转换为插入。

	copy(C.begin(), C.end(), out);
	cout << endl;

//------------------------------------------------------------------------

	string s3("grungy");
	C.insert(s3);
	cout << "Set C after insertion:\n";
	copy(C.begin(), C.end(), out);
	cout << endl;

//------------------------------------------------------------------------

	cout << "Showing a range:\n";
	copy(C.lower_bound("ghost"), C.upper_bound("spook"), out);
	//lower_bound将键作为参数,并返回一个迭代器,该迭代器指向集合中第一个不小于键参数的成员。
	//upper_bound将键作为参数,并返回一个迭代器,该迭代器指向集合中第一个大于键参数的成员。

	cout << endl;
	// cin.get();
	return 0;

}

multimap

  • 可反转的、经过排序的关联容器(有序)
  • 键和值的类型不同
  • 同一个键可能与多个值相关联

//===============================================================
//FileName:
//          multimap.cpp
//Date:
//          2019/11/29
//Author:
//          khoing(https://blog.csdn.net/qq_45391763)
//===============================================================
#include <iostream>
#include <map>
#include <algorithm>

typedef int KeyType;
typedef std::pair<const KeyType, std::string> Pair; //keyType是键类型,dataType是存储的数据类型。
typedef std::multimap<KeyType, std::string> MapCode;

//------------------------------------------------------------------------

int main() {
	using namespace std;

//------------------------------------------------------------------------

	//multimap<int, string> codes;
	//codes.insert(pair<const int, string> (213, "Los"));

//------------------------------------------------------------------------
	MapCode codes;
	codes.insert(Pair(415, "San Francisco"));
	codes.insert(Pair(510, "Oakland"));
	codes.insert(Pair(718, "Brooklyn"));
	codes.insert(Pair(718, "Staten Island"));
	codes.insert(Pair(415, "San Rafael"));
	codes.insert(Pair(510, "Berkeley"));

//------------------------------------------------------------------------

	//count接收键作为参数,并返回具有该键的元素数目
	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;//声明一个multimap容器类型的迭代器
	for (it = codes.begin(); it != codes.end(); ++it)
		cout << "    " << (*it).first << "     "
		<< (*it).second << endl;
	//code.begin 返回pair(一组键值对)
	//成员函数.first表示返回键,.second返回值
//------------------------------------------------------------------------

	//打印codes对象中区号为718的所有城市
	pair<MapCode::iterator, MapCode::iterator> range = codes.equal_range(718);
	//equal_range用键作为参数,返回两个迭代器,它们表示的区间与该键匹配
	//返回两个值,该方法将他们封装在一个pair对象中
	//这里的pair的两个模板参数都是迭代器。

//------------------------------------------------------------------------

	//也可以使用auto
	//auto range = codes.equal_range(718);

//------------------------------------------------------------------------

	cout << "Cities with area code 718:\n";
	for (it = range.first; it != range.second; ++it)
		cout << (*it).second << endl;


//------------------------------------------------------------------------


	return 0;


}

无序关联容器

  • 与关联容器一样,也是将值与键关联起来,并使用键来查找值
  • 关联容器是基于树结构的,而无序关联容器是基于数据结构哈希表的。
  • 旨在提高添加和删除元素的速度以及提高算法的效率。
  • unordered_set、unordered_multiset、unordered_multimap
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值