STL容器之map

#include 
#include 
#include 
using namespace std;


/*
简介:
1.map中所有元素都是pair。
2.pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)。
3.所有元素都会根据元素的 键值 实现自动排序(insert时)。

【本质】:
map/multimap属于关联式容器,底层结构是用二叉树实现。

【优点】:
可以根据key值快速找到value值。

【map和multimap区别】:
map不允许容器中有重复key值元素;
multimap允许容器中有重复key值元素。
*/

void PrintMap(map<int,int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
		cout << "Key:" << it->first << "    " << "Value:" << it->second << endl;		//it指向的是一个对组pair
}

void PrintMap(multimap<int, int>& multi_map)
{
	for (map<int, int>::iterator it = multi_map.begin(); it != multi_map.end(); it++)
		cout << "Key:" << it->first << "    " << "Value:" << it->second << endl;		//it指向的是一个对组pair
}


//Key值如若重复,将插入不进去!
void test00()
{
	map<int, int> map1;
	pair<map<int,int>::iterator,bool> MyPair = map1.insert(pair<int, int>(10, 10));
	if (MyPair.second == true) {
		cout << "First insert Successful!" << endl;
	}
	else {
		cout << "First insert Failed!" << endl;
	}
	MyPair = map1.insert(pair<int, int>(10, 34));		//由于key发生重复,所以插入将失败!
	if (MyPair.second) {
		cout << "Second insert Successful!" << endl;
	}
	else{
		cout << "Second insert Failed!" << endl;
	}
}
//[]形式的讲解
void test000()
{
	map<int, int> map1;
	map1[10] = 10;
	map1[20] = 20;
	map1[30] = 30;
//插入时候,如果发现key不存在,那么将会创建pair插入到容器之中去;
//如果发现key已经存在,那么将会修改已存在的key对应的value值!
//如下所示:
	map1[10] = 1212;		//也即是将键值10对应的value值修改为1212
	PrintMap(map1);

//若果通过[]的形式去访问map中一个不存在的key,
//那么map会将这个访问的key插入到map容器中去,并且实值自动赋值为0。
//如下所示:
	cout << "------------------------" << endl;
	cout << "map1[60]:" << map1[60] << endl;
	PrintMap(map1);

}

//构造初始化
void test01()
{
	map<int, int> map1;			//默认构造
	map<int, int> map2(map1);	//拷贝构造
	map<int, int> map3 = map2;	//重载 = 运算符
}

//插入
void test02()
{
	map<int, int> map1;	//map容器模板参数,第一个参数是key值,第二个参数是value值
//插入数据
	//第一种
	map1.insert(pair<int, int>(10, 10));
	//第二种
	map1.insert(make_pair(20, 20));
	//第三种
	map1.insert(map<int, int>::value_type(30, 30));
	//第四种
	map1[40] = 40;
	//第五种
    map1.insert(map1.begin(), make_pair(2, 5));	//第二种,在指定的迭代器位置插入

	PrintMap(map1);
}

//大小和交换
void test04()
{
	map<int, int> map1;
	map1[10] = 10;
	map1[20] = 20;
	map1[30] = 30;
	cout << "size: " << map1.size() << endl;
	cout << "empty: " << map1.empty() << endl;
	PrintMap(map1);


	cout << "-----------------------" << endl;
	map<int, int> map2;
	map2.swap(map1);
	PrintMap(map2);
	cout << "------------------" << endl;
	PrintMap(map1);
}

//删除操作
void test05()
{
	map<int, int> map1;
	map1.insert(make_pair(10, 10));
	map1.insert(make_pair(20, 20));
	map1.insert(make_pair(30, 30));
	map1.insert(make_pair(40, 40));
	map1.insert(make_pair(50, 50));
	map1.insert(make_pair(60, 60));
	PrintMap(map1);

	cout << "--------删除系列----------" << endl;
	cout << "-------------删除第一个-------------" << endl;
	//map1.clear();				//清除所有元素
	map1.erase(map1.begin());
	PrintMap(map1);

	cout << "---------区间删除--------" << endl;
	map<int, int>::iterator it = map1.begin();
	//map1.erase(it, it + 1);	//同样不支持随机访问
	it++;
	it++;
	map1.erase(map1.begin(), it);
	PrintMap(map1);

	cout << "-----------指定值删除-----------" << endl;
	map1.erase(60);
	PrintMap(map1);
}

//查找和统计
void test06()
{
	map<int, int> map1;
	map1.insert(make_pair(10, 10));
	map1.insert(make_pair(20, 20));
	map1.insert(make_pair(30, 30));
	map1.insert(make_pair(40, 40));

	//find()
	//find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end()
	map<int,int>::iterator it = map1.find(20);
	if (it != map1.end())
		cout << "Yes!" << endl;
	else
		cout << "Not find!" << endl;

	//count()
	//count(key); //统计key的元素个数
	cout << "-----------------key值相同的个数----------------" << endl;
	multimap<int,int> multi_map;
	multi_map.insert(make_pair(10, 10));
	multi_map.insert(make_pair(10, 20));
	multi_map.insert(make_pair(10, 30));
	multi_map.insert(make_pair(40, 40));
	PrintMap(multi_map);
	cout << "count:" << multi_map.count(10) << endl;
}

//排序:默认是升序
//利用仿函数可以改变排序规则
class MyCompare
{
public:
	MyCompare() {};
	bool operator()(int v1, int v2) const
	{
		return v1 > v2;
	}
};
void test07()
{
	map<int, int, MyCompare> map1;
	map1.insert(make_pair(10, 10));
	map1.insert(make_pair(1, 20));
	map1.insert(make_pair(4, 30));
	map1.insert(make_pair(35, 40));
	for (map<int, int, MyCompare>::iterator it = map1.begin(); it != map1.end(); it++)
		cout << "Key:" << it->first << "    " << "Value:" << it->second << endl;
}

int main()
{
	//test00();
	test000();
	//test01();
	//test02();
	//test04();
	//test05();
	//test06();
	//test07();
	return 0;
}

你可能感兴趣的:(C++,c++,开发语言)