C++之关联式容器

注: 以下使用的接口均为部分接口。

1. 序列式容器
回顾:vector,list,dequeue,等都被称为序列式容器,因为其底层都是线性序列的数据结构,里面存储的是元素本身。

2.关联式容器
关联式容器也是用来存储数据的,它与序列式容器不同的是,其里面存储的是<key, value>结构的键值对,在数据检索时比序列式容器效率更高

3. 键值对
用来表示具有一一对应关系的一种结构,该结构中一般只包含两个成员变量key和value, key代表键值,value表示与key对应的关系。 举个例子:现在要建立一个英汉互译字典,那该字典中必然有英文单词与其对应的中文含义。而且,英文单词与其中文含义是一一对应的关系,即通过该单词,在词典中就可以找到与其对应的中文含义。

以下是关于键值对的定义:

template <class T1, class T2>

struct pair
{
	typedef T1 first_type;
	typedef T2 second_type;

	T1 first;
	T2 second;

	pair()
		:first(T1());
	, second(T2());
	{}

	pair(const T1& a, const T2& b)
		:first(a)
		, second(b)
	{}
};

4.树形结构的关联式容器

根据应用场景不同,STL总共实现了两种不同结构的管理式容器:树形结构和哈希结构。树形关联式容器主要有四种:map, set, multimap, multiset.这四种容器共同特点是:使用平衡搜索树(红黑树)作为其底层结构,容器中的元素是一个有序的序列。

(1)map:
a. map是关联式容器,它按照特定的次序(按照key来比较)存储由键值key和值value组合而成的元素。
b. 在map中,键值key通常用于排序和唯一标识元素,而value中存储于此值关联的内容
c. 在内部,map中的元素总是按照键值key进行比较排序的。
d.map支持下标访问符,即在[]中放入key,就可以找到与key对应的value。

应用:

//map的构造
#include <string>
#include <map>
#include <iostream>
#include <stdlib.h>

using namespace std;
void TestMap()
{
	//key和value都给成字符串
	map<string, string> m1;

	map<string, string> m2{ { "apple", "苹果" }, { "banana", "香蕉" }, { "orange", "橘子" }, 
								{ "peach", "桃子" }, { "watermelon", "西瓜" } };
	cout << m2["apple"] << endl;
	cout << m2["watermelon"] << endl;
	map<string, string> m3(m2);
}
int main()
{
	TestMap();
	system("pause");
	return 0;
}

在这里插入图片描述

//map的迭代器
#include <string>
#include <map>
#include <iostream>
#include <stdlib.h>

using namespace std;
void TestMap()
{
	map<string, string> m{ { "apple", "苹果" }, { "banana", "香蕉" }, { "orange", "橘子" },
	{ "peach", "桃子" }, { "watermelon", "西瓜" } };
	for (auto it = m.begin(); it != m.end(); ++it)
		cout << (*it).first << "--->" << it->second << endl;
}
int main()
{
	TestMap();
	system("pause");
	return 0;

}
	

在这里插入图片描述

//map的容量与元素访问
#include <string>
#include <map>
#include <iostream>
#include <stdlib.h>

using namespace std;
void TestMap()
{
	//构造一个空的map,此时m中一个元素也没有
	map<string, string> m;
	/*operator[]原理: 用<key, T()>构造一个键值对,然后调用insert()函数将键值对插入到map中
					  如果key已经存在,插入失败,insert()函数返回该key所在位置的迭代器
					  如果key不存在,插入成功,insert()函数返回新插入元素所在位置的迭代器
					  operator[]函数最后将insert()返回值的键值对的value返回
	*/
	m["apple"] = "苹果";

	//将<"apple", "">插入到map中,插入失败,将<"apple", "苹果">中的“苹果”返回
	cout << m["apple"] << endl;
	cout << m.size() << endl;

	//banana不在map中,该函数抛异常
	//m.at("banana");
}
int main()
{
	TestMap();
	system("pause");
	return 0;

}

在这里插入图片描述

#include <string>
#include <map>
#include <iostream>
#include <stdlib.h>

using namespace std;
void TestMap()
{
	//map中的元素修改
	map<string, string> m;
	//向map中插入元素的方式;
	//将键值对<"peach","桃子">插入到map中,用pair直接来构造键值对
	m.insert(pair<string, string>("peach", "桃子"));
	//将键值对<"peach","桃子">插入到map中,用make_pair直接来构造键值对
	m.insert(make_pair("banana", "香蕉"));
	//借用operator[]向map中插入元素
	m["apple"] = "苹果";

	//key不存在时抛异常
	//m.at("watermelon") = "西瓜";
	m.insert(m.find("banana"), make_pair("watermelon", "西瓜"));
	cout << m.size() << endl;

	//用迭代器遍历map中的元素,可以得到一个按照key排序的序列
	for (auto& e : m)
		cout << e.first << "--->" << e.second << endl;
	cout << endl;

	//map中的键值对key一定是唯一的,如果存在key则插入失败
	auto ret = m.insert(make_pair("peach","桃子"));
	if (ret.second)
		cout << "<peach, 桃子>不在map中,已经插入" << endl;
	

	//删除key为"apple"的元素
	m.erase("apple");
	for (auto&e : m)
		cout << e.first << "--->" << e.second << endl;

	if (1 == m.count("apple"))
		cout << "apple还在" << endl;
	else
		cout << "apple被吃了" << endl;

}
int main()
{
	TestMap();
	system("pause");
	return 0;

}
	

在这里插入图片描述
【总结】

  1. map中的元素是键值对;
  2. map中的key是唯一的,并且不能修改
  3. 默认按照从小到大的顺序对key进行排列
  4. map中的元素如果用迭代器去遍历,可得到一个有序的序列
  5. 底层为平衡搜索树(红黑树),查找效率高(O(logN))
  6. 支持operator[],实际上为插入查找。

(2)multimap
a. multimaps是关联式容器,它按照特定的顺序,存储由key和value映射的键值对,其中多个键值对之间的key是可以重复的。
b. 在multimaps里,通常按照key排序和唯一标识元素,而映射的value存储与key关联的内容。两者的类型可以不同,通过内部成员value_type组合在一起。
c. 在内部,multimaps中的元素总是通过其内部比较对象,按照特定的顺序标准对key进行排序。
d. 使用迭代器直接遍历multimap中的元素可以得到关于key的有序序列。
e. 底层用二叉搜索树(红黑树)实现。
注意:multimap和map的唯一不同是,map中的key是唯一的,而multimap中key可以重复。

应用:

#include <string>
#include <map>
#include <iostream>
#include <stdlib.h>

using namespace std;

void test_multimap1()
{
	multimap<string, string> m;
	m.insert(make_pair("李逵", "黑旋风"));
	m.insert(make_pair("林冲", "豹子头"));
	m.insert(make_pair("鲁达", "花和尚"));

	//尝试插入key相同的元素
	m.insert(make_pair("李逵", "铁牛"));
	cout << m.size() << endl;

	for (auto& e : m)
	{
		cout << "<" << e.first << "," << e.second << ">" << endl;
	}

	//key为李逵的元素有几个
	cout << "李逵:" << m.count("李逵") << endl;


}
int main()
{
	test_multimap1();
	system("pause");
	return 0;

}

在这里插入图片描述

#include <string>
#include <map>
#include <iostream>
#include <stdlib.h>

using namespace std;

void test_multimap2()
{
	multimap<int, int> m;
	for (int i = 0; i < 10; ++i)
		m.insert(pair<int, int>(i, i));

	for (auto&e : m)
		cout << e.first << "--->" << e.second << endl;
	cout << endl;

	//返回m中大于等于5的第一个元素
	auto it = m.lower_bound(5);
	cout << it->first << "--->" << it->second << endl;

	//返回m中大于5的第一个元素
	it = m.upper_bound(5);
	cout << it->first << "--->" << it->second << endl;
}
int main()
{
	test_multimap2();
	system("pause");
	return 0;

}

在这里插入图片描述
(3)set
a. set是按照一定次序存储元素的容器
b. 在set中,元素的value也标识它(value就是key,类型为T),并且每个value必须是唯一的。set中的元素不能在容器中修改(元素总是const),但是可以从容器中插入或删除它们
c. 在内部,set中的元素总是按照其内部比较对象(类型比较)所指示的特定严格弱排序准则进行排序
d. set底层是用二叉搜索树(红黑树)实现

使用:

#include <string>
#include <set>
#include <iostream>
#include <stdlib.h>

using namespace std;

void test_set()
{
	//用数组中的元素构造set
	int array[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0, 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
	set<int> s(array, array + sizeof(array)/sizeof(array[0]));
	cout << s.size() << endl;

	//正向打印set中的元素,从打印结果可以看出:set可以去重
	for (auto& e : s)
		cout << e << " ";
	cout << endl;

	//使用迭代器逆向打印set中的元素
	for (auto it = s.rbegin(); it != s.rend(); ++it)
		cout << *it << " ";
	cout << endl;

	//set中值为3的元素出现了几次
	cout << s.count(3) << endl;

}
int main()
{
	test_set();
	system("pause");
	return 0;
}
	

在这里插入图片描述
(4)multiset
a. multiset是按照特定顺序存储元素的容器,其中元素是可以重复的
b. 在multiset中,元素的value也会识别它(因为multiset中本身存储的就是<value,value>组成的键值对,因此value本身就是key,key就是value,类型为T)multiset元素的值不能在容器中进行修改(因为元素总是const的),但可以从容器中插入或删除
c. 在内部,multiset中的元素总是按照其内部比较规则(类型比较)所指示的特定严格弱排序准则进行排序
d. 使用迭代器遍历时会得到一个有序序列
e. multiset底层结构为二叉搜索树(红黑树)

使用:

#include <string>
#include <set>
#include <iostream>
#include <stdlib.h>

using namespace std;

void test_multiset()
{
	int arr[] = { 2, 1, 3, 9, 6, 0, 5, 8, 4, 7 };

	//注意:multiset在底层实际存储的是<int, int>的键值对
	multiset<int> s(arr, arr + sizeof(arr) / sizeof(arr[0]));
	for (auto& e : s)
		cout << e << " ";
	cout << endl;

	//测试multiset中是否可以存储键值相同的元素
	s.insert(5);
	cout << s.count(5) << endl;
	for (auto& e : s)
		cout << e << " ";
	cout << endl;

	//删除所有值为5的元素
	s.erase(5);
	for (auto& e : s)
		cout << e << " ";
	cout << endl;


}
int main()
{
	test_multiset();
	system("pause");
	return 0;

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值