map容器
map中所有元素都是pair;
pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值);
所有元素都会根据元素的键值自动排序;
map/multimap属于关联式容器,底层结构式用二叉树实现的;
map容器不允许有重复key值元素;
multimap容器允许有重复key元素。
1.构造和赋值
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>
using namespace std;
void printMap(const map<int, int>&m)
{
for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << "\t" << "value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
m.insert(make_pair(4, 40));
printMap(m);
map<int, int>m1(m);
printMap(m1);
map<int, int>m2;
m2 = m1;
printMap(m2);
return;
}
int main()
{
test01();
return 0;
}
运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/a459345cc945452e802136fa9d00ea5f.png)
2.大小和交换
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>
using namespace std;
void printMap(const map<int, int>& m)
{
for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << "\t" << "value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, int>m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
printMap(m);
cout << m.empty() << endl;
if (0 != m.empty())
{
cout << "容器为空" << endl;
return;
}
cout << "容器大小:" << m.size() << endl;
map<int, int>m1;
m1.insert(make_pair(4, 100));
m1.insert(make_pair(5, 200));
m1.insert(make_pair(6, 300));
cout << endl;
cout << "--------交换前--------" << endl;
cout << "m容器元素为:" << endl;
printMap(m);
cout << "m1容器元素为:" << endl;
printMap(m1);
m.swap(m1);
cout << "--------交换后--------" << endl;
cout << "m容器元素为:" << endl;
printMap(m);
cout << "m1容器元素为:" << endl;
printMap(m1);
return;
}
int main()
{
test01();
return 0;
}
运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/84e05f8e75f844a1b5583cbba1319b5d.png)
4.插入和删除
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>
using namespace std;
void printMap(const map<int, int>& m)
{
for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << "\t" << "value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
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));
printMap(m);
m.erase(m.begin());
printMap(m);
m.erase(3);
printMap(m);
m.clear();
printMap(m);
if (0 != m.empty())
{
cout << "容器为空!" << endl;
}
return;
}
int main()
{
test01();
return 0;
}
运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/2e067782c1684243b0d0f4b87895686d.png)
5.查找和统计
工程代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>
using namespace std;
void printMap(const map<int, int>& m)
{
for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << "\t" << "value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
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));
printMap(m);
map<int, int>::iterator pos = m.find(2);
if (m.end() == pos)
{
cout << "没找到" << endl;
}
else
{
cout << "key = " << pos->first << "\t" << "value = " << pos->second << endl;
cout << "key的元素个数:" << m.count(3) << endl;
}
return;
}
int main()
{
test01();
return 0;
}
运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/0054371261234f8f80283bd4da53de5f.png)
6.排序
使用仿函数使其降序排列
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>
using namespace std;
class MyCmp
{
public:
bool operator()(int v1, int v2) const
{
return v1 > v2;
}
};
void printMap(const map<int, int, MyCmp>& m)
{
for (map<int, int, MyCmp>::const_iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << "\t" << "value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, int, MyCmp>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));
printMap(m);
return;
}
int main()
{
test01();
return 0;
}
运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/cb129f2d8eb64079ab8264402ea78356.png)
7.自定义数据类型排序
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<map>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
m_name = name;
m_age = age;
}
string m_name;
int m_age;
};
class MyCmp
{
public:
bool operator()(Person p1, Person p2) const
{
return p1.m_age > p2.m_age;
}
};
void printMap(const map<Person, int, MyCmp>& m)
{
for (map<Person, int, MyCmp>::const_iterator it = m.begin(); it != m.end(); it++)
{
cout << "name = " << it->first.m_name << "\t" << "age = " << it->first.m_age << "\t" << "赏金 = " << it->second << endl;
}
cout << endl;
}
void test01()
{
Person p1("曹操", 28);
Person p2("刘备", 30);
Person p3("赵云", 35);
map<Person, int, MyCmp>m;
m.insert(make_pair(p1, 1000));
m.insert(make_pair(p2, 2000));
m.insert(make_pair(p3, 3000));
printMap(m);
return;
}
int main()
{
test01();
return 0;
}
运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/3015c141859848e4b7c4d5b389403a79.png)