C++map使用

map内部是红黑树,平衡二叉树
key定义后不可修改,value可以修改,元素是自动按照键key进行排序
查找key很快,复杂度log(n)
可以使用下表操作[],但要谨慎使用

头文件#include < map >

构造方法

	map<int, string> myMap;
	//添加数据
	myMap[1] = "one";
	myMap[2] = "two";
	myMap[3] = "three";
	
	//遍历map
	ite = myMap.begin();
	for (ite; ite != myMap.end(); ite++)
	{
		cout << (*ite).first << " " << (*ite).second << "\n";
	}
	
	map<int, string> myMap2(myMap.begin(), myMap.end());//迭代器区间
	map<int, string> myMap3(myMap);//参数是另一个map

每个键值对的键都是唯一的
遍历一直是按照键(first)从小到大排序

在这里插入图片描述

insert()
当插入存在的键值对时,该新的键值对不会被插入,
如果定义是myMap[3] = “THREE”,THREE将会替换掉原值

原型
iterator insert( iterator pos, const pair &val );

	map<int, string> myMap;
	map<int, string>::iterator ite = myMap.begin();
	myMap[1] = "one";
	myMap[2] = "two";
	myMap[3] = "three";
	
	ite = myMap.begin();
	ite++;//不可以ite = ite + 2;
	ite++;
	ite++;
	//在iterator位置插入键值对,返回值是iterator,指向插入的键值对
	map<int, string>::iterator t = myMap.insert(ite, pair<int, string>(4, "four"));
	cout << (*t).first << " " << (*t).second << "\n";

C++map使用_第1张图片
pair insert( const pair &val );
map的元素为pair<键, 值>

	//插入键值对
	myMap.insert(pair<int, string>(8, "eight"));
	//遍历map
	ite = myMap.begin();
	for (ite; ite != myMap.end(); ite++)
	{
		cout << (*ite).first << " " << (*ite).second << "\n";
	}

C++map使用_第2张图片
void insert( input_iterator start, input_iterator end );

	map<int, string> myMap4;
	myMap4[11] = "eleven";
	myMap4[12] = "twelve";

	//插入某个迭代器区间
	myMap.insert(myMap4.begin(), myMap4.end());

	//遍历map
	ite = myMap.begin();
	for (ite; ite != myMap.end(); ite++)
	{
		cout << (*ite).first << " " << (*ite).second << "\n";
	}

C++map使用_第3张图片

**find()**查找key,找到返回该key的迭代器,否则返回map的尾部(end())
iterator find( const KEY_TYPE &key );

	map<int, string> myMap;
	map<int, string>::iterator ite = myMap.begin();
	myMap[1] = "one";
	myMap[2] = "two";
	myMap[3] = "three";
	//查找键为2的元素,找到了返回该迭代器,没找到返回myMap的尾部
	map<int, string>::iterator iteFind = myMap.find(2);
	if (iteFind == myMap.end())
	{
		cout << "没找到该键" << "\n";
	}
	else
	{
		cout << "找到该元素: " << (*iteFind).first << " " << (*iteFind).second << "\n";
	}


erase() 删除键值对
void erase( iterator pos );删除指定位置的键值对
void erase( iterator start, iterator end );删除迭代器某个区间的范围

删除指定key的元素,删除成功返回1,否则返回0
size_type erase( const KEY_TYPE &key );

	map<int, string> myMap;
	map<int, string>::iterator ite = myMap.begin();
	myMap[1] = "one";
	myMap[2] = "two";
	myMap[3] = "three";

	//查找键为2的元素,找到了返回该迭代器,没找到返回myMap的尾部
	map<int, string>::iterator iteFind = myMap.find(2);
	if (iteFind == myMap.end())
	{
		cout << "没找到该键" << "\n";
	}
	else
	{
		cout << "找到该元素: " << (*iteFind).first << " " << (*iteFind).second << "\n";
	}

	myMap.erase(2);
	//遍历map
	ite = myMap.begin();
	for (ite; ite != myMap.end(); ite++)
	{
		cout << (*ite).first << " " << (*ite).second << "\n";
	}

	myMap.erase(myMap.begin());

	//遍历map
	ite = myMap.begin();
	for (ite; ite != myMap.end(); ite++)
	{
		cout << (*ite).first << " " << (*ite).second << "\n";
	}

	myMap.erase(myMap.begin(), myMap.end());
	
	//遍历map
	ite = myMap.begin();
	for (ite; ite != myMap.end(); ite++)
	{
		cout << (*ite).first << " " << (*ite).second << "\n";
	}

在这里插入图片描述
clear()清除所有元素
empty() 判断map是否为空

	myMap.clear();
	if (myMap.empty())
	{
		cout << "map为空" << "\n";
	}
	else
	{
		cout << "map不为空" << "\n";
	}

size()返回元素个数

	int mapLen = myMap.size();
	cout << "元素个数为: " << mapLen << "\n";

count(const KEY_TYPE &key ) 返回键key出现的次数,每个该key则返回0

	int times = myMap.count(2);
	cout << "该键key出现的次数" << times << "\n";

iterator lower_bound( const KEY_TYPE &key );
返回大于等于(>=)该键key下一个元素的迭代器

iterator upper_bound( const KEY_TYPE &key );
返回大于(>)该键key下一个元素的迭代器

	map<int, string> myMap;
	map<int, string>::iterator ite = myMap.begin();
	myMap[1] = "one";
	myMap[2] = "two";
	myMap[3] = "three";
	myMap[4] = "four";
	myMap[5] = "five";
	
	map<int,string>::iterator iteLower = myMap.lower_bound(3);
	cout << "lower_bound: " << (*iteLower).first << "\n";

	map<int, string>::iterator iteUpper = myMap.upper_bound(3);
	cout << "upper_bound: " << (*iteUpper).first << "\n";

在这里插入图片描述

swap()交换两个map所有元素

	map<int, string> myMap;
	map<int, string>::iterator iteMap = myMap.begin();
	myMap[1] = "one";
	myMap[2] = "two";
	myMap[3] = "three";
	myMap[4] = "four";
	myMap[5] = "five";

	map<int, string> myMap2;
	map<int, string>::iterator iteMap2 = myMap2.begin();
	myMap2[1] = "One";
	myMap2[2] = "Two";
	myMap2[3] = "Three";
	
	myMap.swap(myMap2);
	
	//遍历map
	iteMap = myMap.begin();
	for (iteMap; iteMap != myMap.end(); iteMap++)
	{
		cout << (*iteMap).first << " " << (*iteMap).second << "\n";
	}

	cout << "\n";

	//遍历map
	iteMap2 = myMap2.begin();
	for (iteMap2; iteMap2 != myMap2.end(); iteMap2++)
	{
		cout << (*iteMap2).first << " " << (*iteMap2).second << "\n";
	}

C++map使用_第4张图片

你可能感兴趣的:(C/C++)