Map所有的元素都是pair,同时拥有实值和键值,pair的第一元素被视为键值,第二元素被视为实值,map不允许两个元素有相同的键值。
我们可以通过map的迭代器改变map的键值吗?答案是不行,因为map的键值关系到map元素的排列规则,任意改变map键值将会严重破坏map组织。如果想要修改元素的实值,那么是可以的。
Multimap和map的操作类似,唯一区别multimap键值可重复。
Map和multimap都是以红黑树为底层实现机制。
map容器
每个元素 都是一个pair
对于map而言 key是不可以重复
multimap可以
4中插入方式
count 统计 map 0 或1 multimap可能大于1
排序规则自己指定
main.cpp
// 49.map.cpp : 定义控制台应用程序的入口点。
//
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <map>
using namespace std;
/*
map构造函数
map<T1, T2> mapTT;//map默认构造函数:
map(const map &mp);//拷贝构造函数
3.8.2.2 map赋值操作
map& operator=(const map &mp);//重载等号操作符
swap(mp);//交换两个集合容器
3.8.2.3 map大小操作
size();//返回容器中元素的数目
empty();//判断容器是否为空
3.8.2.4 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 test01()
{
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));
//第四种 如果保证key存在 ,那么可以通过[]访问
m[4] = 40;
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value" << it->second << endl;
}
/*输出
key = 1 value10
key = 2 value20
key = 3 value30
key = 4 value40
*/
cout << m[5] << endl;//输出;0
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value" << it->second << endl;
}
/*输出
key = 1 value10
key = 2 value20
key = 3 value30
key = 4 value40
key = 5 value0
*/
//cout << m[4] << endl;//输出;40
if (m.empty())
{
cout << "空" << endl;
}
else
{
cout << "size = " << m.size() << endl;//size = 5
}
}
/*
map删除操作
clear();//删除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg,end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(keyElem);//删除容器中key为keyElem的对组。
3.8.2.6 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 test02()
{
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;
m.erase(1);
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value" << it->second << endl;
}
/*输出
key = 2 value20
key = 3 value30
key = 4 value40
*/
map<int, int>::iterator pos = m.find(2);
if (pos != m.end()) // 找到:key2 value : 20
{
cout << "找到:key" << pos->first << " value:" << pos->second << endl;
}
else
{
cout << "未找到" << endl;
}
int num = m.count(3); //map的count 要么0 要么1
cout << "num = " << num << endl;//num = 1
// lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
map<int, int>::iterator ret = m.lower_bound(3);
if (ret != m.end())//lower_bound 中key3 value: 30
{
cout << "lower_bound 中key" << ret->first << " value: " << ret->second << endl;
}
else
{
cout << "未找到" << endl;
}
//upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
ret = m.upper_bound(3);
if (ret != m.end()) // upper_bound 中key4 value : 40
{
cout << "upper_bound 中key" << ret->first << " value: " << ret->second << endl;
}
else
{
cout << "未找到" << endl;
}
//equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
pair<map<int, int>::iterator, map<int, int>::iterator> ret2 = m.equal_range(3);
if (ret2.first != m.end())//找到了equal_range 中的lower_bound 的key 3 value: 30
{
cout << "找到了equal_range 中的lower_bound 的key " << ret2.first->first << " value: " << ret2.first->second << endl;
}
else
{
cout << "未找到" << endl;
}
if (ret2.second != m.end())//找到了equal_range 中的upper_bound 的key 4 value: 40
{
cout << "找到了equal_range 中的upper_bound 的key " << ret2.second->first << " value: " << ret2.second->second << endl;
}
else
{
cout << "未找到" << endl;
}
}
//指定排序规则
class myCompare
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void test03()
{
//从大到小排序
map<int, int, myCompare> 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, myCompare>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key: " << it->first << " value: " << it->second << endl;
}
/*输出
key: 4 value: 40
key: 3 value: 30
key: 2 value: 20
key: 1 value: 10
*/
}
int main(){
//test01();
//test02();
test03();
system("pause");
return EXIT_SUCCESS;
}