map基本概念
简介:
- map中所有元素都是pair
- pair中第一个元素为key(键值),起到索引的作用,第二个元素为value(实值)
- 所有元素都会根据元素的键值自动排序
本质:
- map/multimap属于关联式容器,底层是用二叉树实现
优点:
map和multimap区别:
- map不允许容器中有重复的key值
- multimap允许容器中有重复的key值
map的构造和赋值
打印map容器
void print(map<int, int> &m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it)
{
cout << "key =" << it->first << " value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, int> m;
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(4, 10));
m.insert(pair<int, int>(3, 30));
print(m);
map<int, int> m1(m);
print(m1);
map<int, int> m2;
m2 = m1;
print(m2);
}
运行结果:
key =1 value = 10
key =2 value = 20
key =3 value = 30
key =4 value = 10
key =1 value = 10
key =2 value = 20
key =3 value = 30
key =4 value = 10
key =1 value = 10
key =2 value = 20
key =3 value = 30
key =4 value = 10
map中元素都是成对出现的,插入数据时要使用对组
map大小和交换
函数原型:
size(); //返回元素的数目
empty(); //判断容器是否为空
swap(st); //交换两个集合容器
void test02()
{
map<int, int> m;
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(1, 10));
if (m.empty())
{
cout << "m is empty" << endl;
}
else
{
cout << "m is not empty" << endl;
cout << "size of m is " << m.size() << endl;
}
map<int, int> m1;
m1.insert(pair<int, int>(2, 200));
m1.insert(pair<int, int>(1, 100));
m1.insert(pair<int, int>(4, 400));
m1.insert(pair<int, int>(3, 300));
cout << "交换前:" << endl;
print(m1);
m1.swap(m);
cout << "交换后:" << endl;
print(m1);
}
运行结果:
m is not empty
size of m is 3
交换前:
key =1 value = 100
key =2 value = 200
key =3 value = 300
key =4 value = 400
交换后:
key =1 value = 10
key =2 value = 20
key =3 value = 30
map插入和删除
功能描述:
函数原型:
insert(); //在容器中插入元素
clear(); //清除所有元素
erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg,end); //删除区间[beg,end)的所有元素,返回下一元素的迭代器
erase(key); //删除容器中值为key的元素
void test03()
{
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;
print(m);
m.erase(1);
print(m);
m.erase(m.begin());
print(m);
m.erase(m.begin(), m.end());
print(m);
}
运行结果:
key =1 value = 10
key =2 value = 20
key =3 value = 30
key =4 value = 40
key =2 value = 20
key =3 value = 30
key =4 value = 40
key =3 value = 30
key =4 value = 40
map查找和统计
功能描述:
函数原型:
find(key); //查找key是否存在,返回该键的元素迭代器;若存在,返回map.end();
count(key); //统计key的个数
void test04()
{
map<int, int> m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
m.insert(make_pair(4, 40));
map<int, int>::iterator it = m.find(1);
if (it != m.end())
{
cout << "找到了元素" << endl;
cout << it->first << " " << it->second << endl;
}
else
{
cout << "没找到元素" << endl;
}
int n = m.count(1);
cout << n << endl;
}
运行结果:
找到了元素
1 10
1
map容器排序
map容器默认排序规则为 按照key的值进行从小到大排序,如何改变规则? 利用仿函数,可以改变排序规则
class MyCompare
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};
void test05()
{
map<int, int, MyCompare> m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(4, 40));
m.insert(make_pair(3, 30));
for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); ++it)
{
cout << "key =" << it->first << " value = " << it->second << endl;
}
cout << endl;
}
运行结果:
key =4 value = 40
key =3 value = 30
key =2 value = 20
key =1 value = 10
|