STL map容器

map容器

概述

Map的特性是,所有元素都会根据元素的键值自动排序。Map所有的元素都是pair,同时拥有实值和键值,pair的第一元素被视为键值,第二元素被视为实值,map不允许两个元素有相同的键值。

Map的特性是,所有元素都会根据元素的键值自动排序。Map所有的元素都是pair,同时拥有实值和键值,pair的第一元素被视为键值,第二元素被视为实值,map不允许两个元素有相同的键值。

  • 关联式容器

  • 会自动按照key做排序

  • map不允许有重复的key值

  • Multimap和map的操作类似,唯一区别multimap键值可重复。

  • Map和multimap都是以红黑树为底层实现机制。

常用API

map插入数据元素操作

/*
	map构造函数
	map<T1, T2> mapTT;//map默认构造函数:
	map(const map &mp);//拷贝构造函数

	map赋值操作
	map& operator=(const map &mp);//重载等号操作符
	swap(mp);//交换两个集合容器
*/
/*
	map大小操作
	size();//返回容器中元素的数目
	empty();//判断容器是否为空

	map插入数据元素操作
	map.insert(...); //往容器插入元素,返回pair<iterator,bool>
	map<int, string> mapStu;
	// 第一种 通过pair的方式插入对象
	mapStu.insert(pair<int, string>(3, "小张"));
	// 第二种 通过pair的方式插入对象
	mapStu.inset(make_pair(-1, "校长"));
	// 第三种 通过value_type的方式插入对象
	mapStu.insert(map<int, string>::value_type(1, "小李"));
	// 第四种 通过数组的方式插入值
	mapStu[3] = "小刘";
	mapStu[5] = "小王";
*/
void test()
{
	map<int,int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(make_pair(2, 20));

	m.insert(map<int, int>::value_type(3, 30));
	m[4] = 40;

	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << it->first << " :" << it->second << " " << endl;
	}
}
 

map删除操作

/*
	map删除操作
	clear();//删除所有元素
	erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
	erase(beg,end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
	erase(keyElem);//删除容器中key为keyElem的对组。
	
*/
void test()
{
	map<int, int>m;
	m.insert(make_pair(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(map<int, int>::value_type(4, 40));
	m[3] = 30;
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << it->first << " :" << it->second << " " << endl;
	}

	m.erase(3);
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << it->first << " :" << it->second << " " << endl;
	}

	m.erase(m.begin());
	m.clear();

	cout << m.size() << endl;
}

map查找操作

/*

	find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;/若不存在,返回map.end();
	count(keyElem);//返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。
	lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
	upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
	equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。

*/

void test()
{
	map<int, int>m;
	m.insert(make_pair(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(map<int, int>::value_type(3, 30));
	m[4] = 40;

	map<int, int>::iterator it = m.find(3);
	if (it != m.end())
	{
		cout << "找到了元素" << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}
	map<int, int>::iterator itpos = m.lower_bound(3);
	if (itpos != m.end())
	{
		cout << "找到了lower_bound元素 key:" <<itpos->first <<" val:" <<itpos->second << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}

	itpos = m.upper_bound(3);
	if (it != m.end())
	{
		cout << "找到了upper_bound元素 key:" << itpos->first << " val:" << itpos->second << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}

	pair<map<int, int>::iterator, map<int, int >::iterator > p = m.equal_range(3);
	if (p.first != m.end())
	{
		cout << "equal_range 找到了lower_bound元素 key:" << p.first->first << " val:" << p.first->second << endl;
	}

	if (p.second != m.end())
	{
		cout << "equal_range 找到了upper_bound元素 key:" << p.second->first << " val:" << p.second->second << endl;
	}

}

利用仿函数实现自定义排序

//利用仿函数实现自定义排序
class compareInt
{
public: 
	bool operator()(int v1, int v2)
	{
		return v1 < v2;
	}
};
void test()
{
	map<int, int, compareInt>m;
	m.insert(make_pair(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(map<int, int>::value_type(3, 30));
	m[4] = 40;

	for (map<int, int, compareInt>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << it->first << " :" << it->second << " " << endl;
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值