map容器

map/multimap容器

map/multimap基本概念
map的特性是,所有元素都会根据元素的键值自动排序。map所有的元素都是pair;同时拥有实值和键值,pair的第一元素被视为键值,第二元素被视为实值,map不允许两个元素有相同的键值。
我们可以通过map的迭代器改变map的键值吗?答案是不行,因为map的键值关系到map元素的排列规则,任意改变map键值将会更严重的破坏map组织,如果想要修改元素的实值,那么是可以的。
map和list拥有相同的某些特性,当对它的容器元素进行新增操作或者删除操作时,操作之前的所有迭代器,在操作完成后依然有效,当然被删除的那个元素的迭代器必然是个例外。
multimap和map的操作类似,唯一区别就是multimap键值可以重复。
map和multimap都是以红黑树为底层实现机制。

map和multimap常用API

map构造函数:
mapmapTT; //map默认构造函数
map(const map &mp); //拷贝构造函数

map赋值操作
map & operator = (const map &mp); //重载等号操作符
swap(mp); //交换两个集合容器

map大小操作
size(); //返回容器中元素的数目
empty(); //判断容器是否为空

map插入数据元素操作
map.insert(…); //往容器插入元素,返回pair
mapmapStu;
//第一种,通过pair的方式插入
mapStu.insert(pair(3,“xiao”));
//第二种通过pair的方式插入
mapstu.insert(make_pair(3,“xiao”));
//第三种通过value_type 的方式插入
mapStu.insert(map::value_type(3,“xiao”));
//通过数组的方式插入
mapStu[3] = “xiao”;
mapStu[5] = “da”;

//An highlighted block
void test1()
	{
	map<int,int> m;
	//插入值  4种方式
	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;			//如果保证key存在,那么可以使用

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

	m[5];

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

	m[5] = 50;
	m[4] = 400;
	cout << m[5] << endl;
	cout << m[4] << endl;


	if (m.empty())
	{
		cout << "map为空" << endl;
	} 
	else
	{
		cout << "map不为空" << endl;
	}


	}

map容器_第1张图片map删除操作:
clear(); //删除所有元素
erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg,end); //删除区间(beg,end)的所有元素,返回下一个元素的迭代器
erase(key,elem); //删除容器中key为keyElem的对组

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

//An highlighted block
void test2()
	{
		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 << "key: " << it->first << "  value: " << it->second << endl;
			}
		//删除
		m.erase(1);
		cout << "**************************************" << endl;
		for (map<int,int>::iterator it = m.begin();it != m.end();it++)
			{
			cout << "key: " << it->first << "  value: " << it->second << endl;
			}
		//查找
		map<int,int>::iterator pos = m.find(1);
		if (pos != m.end())
		{
			cout << "找到了,对应的key值是:" << pos->first << "  value:" << pos->second << endl;
		} 
		else
		{
			cout << "没有找到"  << endl;
		}
		cout << "***************************" << endl;

		int num = m.count(2);
		cout << "num:" << num << endl;


		//lower_bound(keyElem);		//返回第一个key >= keyElem 的元素迭代器

		map<int,int>::iterator lower_pos = m.lower_bound(3);
		if (lower_pos != m.end())
			{
			cout << "找到了,lower_bound对应的key值是:" << lower_pos->first << "  value:" << lower_pos->second << endl;
			} 
		else
			{
			cout << "没有找到"  << endl;
			}
		cout << "***************************" << endl;


		//upper_bound(keyElem);		//返回第一个key > keyElem的元素迭代器
		map<int,int>::iterator upper_pos = m.upper_bound(3);
		if (upper_pos != m.end())
			{
			cout << "找到了,upper_pos对应的key值是:" << upper_pos->first << "  value:" << upper_pos->second << endl;
			} 
		else
			{
			cout << "没有找到"  << endl;
			}
		cout << "***************************" << endl;

		//equal_range(keyElem);		//返回key与keyElem相等的上下限的两个迭代器
		pair<map<int,int>::iterator,map<int,int>::iterator> ret = m.equal_range(3);
		//取上限的值
		if ( ret.first != m.end())
			{
			cout << "找到了,equal_range中lower_bound对应的key值是:" << ret.first ->first<< "  value:" << ret.first ->second<< endl;
			} 
		else
			{
			cout << "没有找到"  << endl;
			}
		cout << "***************************" << endl;

		//取下限的值
		if ( ret.second != m.end())
			{
			cout << "找到了,equal_range中upper_bound对应的key值是:" << ret.second ->first<< "  value:" << ret.second ->second<< endl;
			} 
		else
			{
			cout << "没有找到"  << endl;
			}
		cout << "***************************" << endl;


	}

map容器_第2张图片

//An highlighted block

//指定排序规则

class my_compare
	{
	public:
	bool operator ()(int val1,int val2)
		{
		return val1 > val2;
		}
	};
void test3()
	{
	//从大到小排序
	map<int,int,my_compare> m;
	m.insert(pair<int,int>(1,100));
	m.insert(make_pair(2,20));
	m.insert(map<int,int>::value_type(3,30));
	m[4] = 40;
	for (map<int,int,my_compare>::iterator it = m.begin();it != m.end();it++)
		{
		cout << "key: " << it->first << "  value: " << it->second << endl;
		}
	}

map容器_第3张图片

你可能感兴趣的:(c++)