使用C++中map/multimap容器时必须包含
#include <map>
using namespace std;
map基本概念
map中所有元素都是pair;
pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值);
所有元素都会根据元素的键值自动排序;
本质
map/multimap属于关联式容器,底层结构是用二叉树实现的
优点:
可以根据key值快速找到value值
map和multimap区别:
map不允许容器中有重复key值元素
multimap允许容器中有重复key值元素
map构造和赋值
①map<T1,T2> mp;
默认构造函数
②map(const map &m);
拷贝构造函数
③map &operator=(const map &m);
重载=操作符
void printMap(map<int, int> &m) {
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << "键值:" << it->first << "实值:" << it->second << endl;
}
}
void test() {
map<int, int> m;
m.insert(pair<int, int>(1, 10)); //匿名对组
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(2, 10));
m.insert(pair<int, int>(4, 10));
printMap(m);
map<int, int>m1(m);
printMap(m1);
map<int, int> m2;
m2 = m;
printMap(m2);
}
map大小和交换
①size();
返回容器中元素的数目
②empty();
判断容器是否为空
③swap(s);
交换两个容器中的元素
void printMap(map<int, int> &m) {
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << "键值:" << it->first << "实值:" << it->second << endl;
}
}
void test() {
map<int, int> m;
m.insert(pair<int, int>(1, 10)); //匿名对组
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(2, 10));
m.insert(pair<int, int>(4, 10));
if (m.empty()) {
cout << "m为空" << endl;
}
else {
cout << "m不为空" << endl;
cout << "m的大小" << m.size() << endl;
}
map<int, int> m1;
m1.insert(pair<int, int>(8, 70)); //匿名对组
m1.insert(pair<int, int>(6, 70));
m1.insert(pair<int, int>(5, 70));
m1.insert(pair<int, int>(7, 70));
m1.swap(m);
printMap(m);
printMap(m1);
}
map插入和删除
①insert(elem);
在容器中插入元素
②clear();
清空容器
③erase(pos);
删除pos迭代器所指的元素,返回下一个元素的迭代器
④erase(beg,end);
删除区间[begin,end)的所有元素,返回下一个元素的迭代器
⑤erase(key);
删除容器中键值为key的元素
void printMap(map<int, int> &m) {
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << "键值:" << it->first << "实值:" << it->second << endl;
}
}
void test() {
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;
//[]不建议插入,可以利用key访问到value
printMap(m);
m.erase(m.begin());
m.erase(3);
m.erase(m.begin(), m.end());
m.clear();
}
map查找和统计
①find(key);
查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回set.end();
②count(key);
统计key元素的个数
void test() {
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(2, 10));
m.insert(pair<int, int>(4, 10));
map<int,int>::iterator pos = m.find(3);
if (pos != m.end()) {
cout << "找到了"<<endl;
}
else {
cout << "没找到" << endl;
}
int num = m.count(3);
cout << num << endl;
}
map容器排序
class Mycompare {
public:
bool operator()(int v1, int v2) {
return v1 > v2;
}
};
void test() {
map<int, int,Mycompare> m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 10));
m.insert(pair<int, int>(2, 10));
m.insert(pair<int, int>(4, 10));
for (map<int, int, Mycompare>::iterator it = m.begin(); it != m.end(); it++) {
cout << "键值:" << it->first << "实值:" << it->second << endl;
}
}
自定义数据类型按键值
class Person {
public:
Person(string name, int age, int height) {
this->m_Name = name;
this->m_Age = age;
this->m_Height = height;
}
string m_Name;
int m_Age;
int m_Height;
};
class MyCompare {
public:
bool operator()(int v1, int v2) {
return v1 > v2;
}
};
void test01() {
map<int, Person,MyCompare> mp;
Person p1("Tom", 18, 180);
Person p2("Jerry", 28, 150);
Person p3("Jeck", 17, 195);
mp.insert(pair<int, Person>(3, p3));
mp.insert(make_pair(1, p1));
mp.insert(make_pair(2, p2));
for (map<int, Person, MyCompare>::iterator it = mp.begin(); it != mp.end(); it++) {
cout << "键值" << it->first << "姓名:" << it->second.m_Name << "年龄:" << it->second.m_Age << "身高:" << it->second.m_Height << endl;
}
}
补充:
在map中键值和实值是映射关系,有以下用法:
map<int, int> m;
m.insert(pair<int,int>(1, 2));
cout << m[1] << endl;
输出为2