MAP容器的简单使用

MAP容器的简单使用

map 相对于set 区别,map 具有键值和实值,所有元素根据键值自动排序。pair 的第
一元素被称为键值,第二元素被称为实值。map 也是以红黑树为底层实现机制。
在这里插入图片描述
对组(pair)将一对值组合成一个值,这一对值可以具有不同的数据类型,两个值可以分
别用pair 的两个公有函数first 和second 访问。
类模板:template <class T1, class T2> struct pair.
二话不说直接上代码,好好领悟。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
using namespace std;
//map容器初始化
void test01(){
	//map容器模板参数,第一个参数key的类型,第二参数value类型
	map<int, int> mymap;

	//插入数据  pair.first key值 piar.second value值
	//第一种
	pair<map<int, int>::iterator, bool> ret = mymap.insert(pair<int, int>(10, 10));
	if (ret.second){
		cout << "第一次插入成功!" << endl;
	}
	else{
		cout << "插入失败!" << endl;
	}
	ret = mymap.insert(pair<int, int>(10, 20));
	if (ret.second){
		cout << "第二次插入成功!" << endl;
	}
	else{
		cout << "插入失败!" << endl;
	}
	//第二种
	mymap.insert(make_pair(20, 20));
	//第三种
	mymap.insert(map<int, int>::value_type(30,30));
	//第四种
	mymap[40] = 40;
	mymap[10] = 20; 
	mymap[50] = 50;
	//发现如果key不存在,创建pair插入到map容器中
	//如果发现key存在,那么会修改key对应的value
	//打印
	for (map<int, int>::iterator it = mymap.begin(); it != mymap.end();it ++){
		// *it 取出来的是一个pair
		cout << "key:" << (*it).first << " value:" << it->second << endl;
	}
	//如果通过【】方式去访问map中一个不存在key,
	//那么map会将这个访问的key插入到map中,并且给value一个默认值
	cout << " mymap[60]: " <<  mymap[60] << endl;
	//打印
	for (map<int, int>::iterator it = mymap.begin(); it != mymap.end(); it++){
		// *it 取出来的是一个pair
		cout << "key:" << (*it).first << " value:" << it->second << endl;
	}
}
class MyKey{
public:
	MyKey(int index,int id){
		this->mIndex = index;
		this->mID = id;
	}
public:
	int mIndex;
	int mID;
};

struct mycompare{
	bool operator()(MyKey key1, MyKey key2){
		return key1.mIndex > key2.mIndex;
	}
};
void test02(){
	map<MyKey, int, mycompare> mymap; //自动排序,自定数据类型
	mymap.insert(make_pair(MyKey(1, 2), 10));
	mymap.insert(make_pair(MyKey(4, 5), 20));
	for (map<MyKey, int, mycompare>::iterator it = mymap.begin(); it != mymap.end();it ++){
		cout << it->first.mIndex << ":" << it->first.mID << " = " << it->second << endl;
	}
}
//equal_range
void test03(){
	map<int, int> mymap;
	mymap.insert(make_pair(1, 4));
	mymap.insert(make_pair(2, 5));
	mymap.insert(make_pair(3, 6));
	pair<map<int, int>::iterator, map<int, int>::iterator> ret =  mymap.equal_range(2);
	if (ret.first != mymap.end()){
		cout << "找到lower_bound!" << endl;
	}
	else{
		cout << "没有找到";
	}
	if (ret.second != mymap.end()){
		cout << "找到upper_bound!" << endl;
	}
	else{
		cout << "没有找到";
	}
}
int main(void){	
	//test01();
	//test02();
	test03();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42063047/article/details/83513370