STL Map学习总结

1、 Map是关联容器,以键值对的形式进行存储,方便进行查找,关键词起到索引的作用,值则表示与索引相关联的数据,以红黑树的结构实现,插入删除等操作都可以在O(log n)时间内完成

2、它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。

//map关联容器
//提供一对一的关系,一个map项包括两个数据段key-value
//key:关键字,一个key只能在map中出现一次(重复出现则覆盖)
//map会根据键值自动排序
//map.find(key)
//map.count(key)计算key出现1次
//multimap.count(key) 可出现多次
//multimap.lower_bound(key) 第一个大于等于key的元素,这个函数用来返回要查找关键字的下界(是一个迭代器)
//multimap.upper_bound(key) 第一个大于key的元素,这个函数用来返回要查找关键字的上界(是一个迭代器)
//equal_range()函数返回一个pair,
	//pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,
	//如果这两个迭代器相等的话,则说明map中不出现这个关键字
//multimap可以有一对多的关系,没有[]操作
//二叉树:一个结点最多有两个子结点
//平衡二叉树:左树和右树的高度绝对值不超过1
//1、使用map需包含map类所在的头文件:#include <map> //注意,STL头文件没有扩展名.h
#include<iostream>
#include<string>
#include<map>

using namespace std;
int main()
{
	//2、map的定义
	map<int, string> map1;
	//3、map四种插入方法
	map1.insert(map<int,string>::value_type(10,"aaaa"));
	map1.insert(pair<int, string>(20, "bbbb"));
	map1.insert(make_pair(30, "cccc"));
	map1[40] = "dddd";
	map1[40] = "eeee";  //覆盖dddd
	for (int i = 10; i < 50; i += 10)
	{
		cout << map1[i]<< endl;
	}
	//4、迭代输出。若为rbegin()和rend()为反向迭代
	for (map<int, string> ::iterator it = map1.begin(); it != map1.end(); it++)
	{
		cout << it->first << ":" << it->second << endl;
	}
	cout << map1.size()<<endl;
	//5、利用find(key)和count(key)来发现一个键是否存在
	//map1.find(key)  返回一个迭代器指向键值为key的元素,如果没有找到,返回指向map尾部的迭代器
	//cout << map1.count(40) << endl;
	map<int, string> ::iterator find_it = map1.find(40);
	cout << find_it->first << ":" << find_it->second << endl;
	//6、元素删除:先查找元素,map<int ,string>::iterator it=map1.find(key); 找到之后map1.erase(it);
	//erase() 的返回值为0或者1.若为1表示删除成功,否则删除失败
	map<int, string> ::iterator del_it = map1.find(10);
	map1.erase(del_it);
	//7、map中的swap函数,交换的是两个容器而不是一个容器中的元素交换
	map<int, int> m1, m2,m3;
	m1.insert(pair<int, int>(1, 1));
	m1.insert(pair<int, int>(2, 2));
	m1.insert(pair<int, int>(3, 3));
	m2.insert(pair<int, int>(11, 11));
	m2.insert(pair<int, int>(12, 12));
	m2.insert(pair<int, int>(13, 13));
	m3.insert(pair<int, int>(21, 21));
	m3.insert(pair<int, int>(22, 22));
	m3.insert(pair<int, int>(23, 23));
	cout << "m1:" << endl;
	for (map<int, int> ::iterator it = m1.begin(); it != m1.end(); it++)
	{
		cout << it->first << ":" << it->second << endl;
	}
	cout << "m2:" << endl;
	for (map<int, int> ::iterator it = m2.begin(); it != m2.end(); it++)
	{
		cout << it->first << ":" << it->second << endl;
	}
	cout << "m3:" << endl;
	for (map<int, int> ::iterator it = m3.begin(); it != m3.end(); it++)
	{
		cout << it->first << ":" << it->second << endl;
	}
	cout << "-----m1.swap(m2)测试-------" << endl;
	m1.swap(m2); //深拷贝。用m1.swap(m2)无法换回来
	cout << "m1:" << endl;
	for (map<int, int> ::iterator it = m1.begin(); it != m1.end(); it++)
	{
		cout << it->first << ":" << it->second << endl;
	}
	cout << "m2:" << endl;
	for (map<int, int> ::iterator it = m2.begin(); it != m2.end(); it++)
	{
		cout << it->first << ":" << it->second << endl;
	}
	
	cout << "-----swap(m3, m1);测试-------" << endl;
	swap(m1, m3);
	cout << "m1:" << endl;
	for (map<int, int> ::iterator it = m1.begin(); it != m1.end(); it++)
	{
		cout << it->first << ":" << it->second << endl;
	}
	cout << "m3:" << endl;
	for (map<int, int> ::iterator it = m3.begin(); it != m3.end(); it++)
	{
		cout << it->first << ":" << it->second << endl;
	}
	//8、sort函数,因为map中key按照升序进行排列的,所以不能使用sort函数
	//9、数据的清空与判空清空map中的数据可以用clear()函数,
	//10、判定map中是否有数据可以用empty()函数,它返回true则说明是空map
	/*map<string, string> map2;
	map2["张三"] = "小三";
	map2["李四"] = "小四";
	cout << map2["张三"] << endl;*/
	cout << "-----------------------multimap分割线-----------------------" << endl;
	multimap<int, string> multimap1;	
	multimap1.insert(make_pair(20, "BBB"));
	multimap1.insert(make_pair(30, "CCC"));
	multimap1.insert(make_pair(50, "EEE"));
	multimap1.insert(make_pair(50, "FFF"));
	multimap1.insert(make_pair(50, "GGG"));
	multimap1.insert(make_pair(50, "HHH"));
	multimap1.insert(make_pair(40, "DDD"));
	multimap1.insert(make_pair(10, "AAA"));

	cout << "multimap1元素为:" << endl;
	for (multimap<int, string> ::iterator it = multimap1.begin(); it != multimap1.end(); it++)
	{
		cout << it->first << ":" << it->second << endl;
	}

	cout << multimap1.count(50) << endl;
	multimap<int, string>::iterator it1 = multimap1.find(50);
	int count =multimap1.count(50);
	cout << "count测试" << endl;
	for (int i = 0; i < count; i++)
	{
		cout << it1->first << ":" << it1->second << endl;
		it1++;
	}
	multimap<int, string> ::iterator it;
	multimap<int, string>::iterator begin, end;
	cout << "lower_bound.upper_bound测试" << endl;
	begin = multimap1.lower_bound(50);
	end = multimap1.upper_bound(50);
	
	for (it = begin; it != end; ++it)
	{
		cout << it->first << ":" << it->second << endl;
		it++;
	}
	cout << "equal_range测试" << endl;
	auto pairTemp=multimap1.equal_range(50);
	begin = pairTemp.first;
	end = pairTemp.second;
	for (it = begin; it != end; ++it)
	{
		cout << it->first << ":" << it->second << endl;
		it++;
	}
	return 0;
}

运行结果:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chde2Wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值