原文链接:https://blog.csdn.net/wtzhu_13/article/details/105472497
STL-map/multimap容器
一、map基本概念
简介
map中所有元素都是pair
pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
所有元素都会根据元素的键值自动排序
map和multimap区别:
map不允许容器中有重复key值元素
multimap允许容器中有重复key值元素
了解Python的同学可能知道这个和Python里的字典类型对应
二、map的常用API
API | 功能 | 使用 |
---|---|---|
size() | 返回容器中元素的数目 | map1.size() |
empty() | 判断容器是否为空 | map1.empty |
swap(st) | 交换两个集合容器 | map1.swap(map2) |
insert(elem) | 在容器中插入元素 | map1.insert(elem) |
clear() | 清空容器 | map1.clear() |
erase() | 删除容器中的元素 | map1.erase(pos)删除指定位置的元素,pos为迭代器类型;map.erase(beg, end)删除指定区间元素,返回一个元素的迭代器;map.earse(key)删除容器中值为key的元素 |
find(key) | 查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end(); | map<int, int>::iterator pos = m.find(key); |
count(key) | 统计key的元素个数 | int num = m.count(3); |
advance(iterator) | 改变迭代器的指向 | map<int, Person>::iterator it = m.begin();advance(it, 3); // 指向第4个位置 |
三、map的插入
-
key值为普通类型时候,对于定义的<int, int>类型的map的四种插入值得方式。
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="c++" cid="n62" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">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; </pre> -
当key值为自定数据类型时:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="C++" cid="n66" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">#include <iostream>
include <set>
include <string>
include <map>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test01()
{
map<Person, int> m;
Person p1("刘备", 23);
Person p2("关羽", 27);
Person p3("张飞", 25);
Person p4("赵云", 21);
m.insert(pair<Person, int>(p1, 1));
m.insert(pair<Person, int>(p3, 3));
m.insert(pair<Person, int>(p4, 4));
m.insert(pair<Person, int>(p2, 2));
for (map<Person, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "姓名: " << it->first.m_Name << " 年龄: " << it->first.m_Age << endl;
}
}
int main() {
test01();
return 0;
}
********************输出结果*************************
...
error: invalid operands to binary expression ('const Person' and 'const Person')
...</pre><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="C++" cid="n71" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">#include <iostream>
include <set>
include <string>
include <map>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class MyCompare {
public:
bool operator()(const Person & p1, const Person & p2) const {
return p1.m_Age < p2.m_Age;
}
};
void test01()
{
map<Person, int, MyCompare> m;
Person p1("刘备", 23);
Person p2("关羽", 27);
Person p3("张飞", 25);
Person p4("赵云", 21);
m.insert(pair<Person, int>(p1, 1));
m.insert(pair<Person, int>(p3, 3));
m.insert(pair<Person, int>(p4, 4));
m.insert(pair<Person, int>(p2, 2));
for (map<Person, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "姓名: " << it->first.m_Name << " 年龄: " << it->first.m_Age << endl;
}
}
int main() {
test01();
return 0;
}
********************输出结果*************************
(base) BigfishdeMacBook-Pro:STL bigfish ./a.out
姓名: 赵云 年龄: 21
姓名: 刘备 年龄: 23
姓名: 张飞 年龄: 25
姓名: 关羽 年龄: 27
(base) BigfishdeMacBook-Pro:STL bigfish$</pre>-
在类里重载小于号< 注意只重载小于号,不要去重载大于号,因为map内部是调用<号, 如果想改变为 升 / 降序列,只需改变判断条件即可
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="c++" cid="n76" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">#include <iostream>
include <set>
include <string>
include <map>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
bool operator < (Person const& intplus)const
{
return m_Age < intplus.m_Age;
}
string m_Name;
int m_Age;
};
void test01()
{
map<Person, int> m;
Person p1("刘备", 23);
Person p2("关羽", 27);
Person p3("张飞", 25);
Person p4("赵云", 21);
m.insert(pair<Person, int>(p1, 1));
m.insert(pair<Person, int>(p3, 3));
m.insert(pair<Person, int>(p4, 4));
m.insert(pair<Person, int>(p2, 2));
for (map<Person, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "姓名: " << it->first.m_Name << " 年龄: " << it->first.m_Age << endl;
}
}
int main() {
test01();
return 0;
}
********************输出结果*************************
(base) BigfishdeMacBook-Pro:STL bigfish ./a.out
姓名: 赵云 年龄: 21
姓名: 刘备 年龄: 23
姓名: 张飞 年龄: 25
姓名: 关羽 年龄: 27
(base) BigfishdeMacBook-Pro:STL bigfish$</pre>如果需要更加严谨就应该在重载方法中进一步判断,因为这样如果某个属性等的话,就只能插入一个进去,因为map的key不能重复,如果是multimap就不用担心这点。
利用仿函数operator ()定义一个排序规则
直接使用insert会报错,错误信息太长,粘贴了一句,具体解决方案:
-
四、map的删除
第一种通过key值删除,不多说,直接在参数中传入需要删除的key值就好。
第二种通过迭代器来删除指定位置或者指定区间的元素。
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="c++" cid="n94" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">#include <iostream>
include <set>
include <string>
include <map>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test01()
{
map<int, Person> m;
Person p1("刘备", 23);
Person p2("关羽", 27);
Person p3("张飞", 25);
Person p4("赵云", 21);
m.insert(pair<int, Person>(1, p1));
m.insert(pair<int, Person>(3, p3));
m.insert(pair<int, Person>(4, p4));
m.insert(pair<int, Person>(2, p2));
for (map<int, Person>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "姓名: " << it->second.m_Name << " 年龄: " << it->second.m_Age << endl;
}
m.erase(m.begin());
cout << "删除后:" << endl;
for (map<int, Person>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "姓名: " << it->second.m_Name << " 年龄: " << it->second.m_Age << endl;
}
}
int main() {
test01();
return 0;
}
********************输出结果************************
(base) BigfishdeMacBook-Pro:STL bigfish</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="c++" cid="n103" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">map<int, Person>::iterator it = m.begin();
advance(it, 3); // 指向第4个位置
m.erase(it);</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="c++" cid="n84" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">#include <iostream>
include <set>
include <string>
include <map>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test01()
{
map<int, Person> m;
Person p1("刘备", 23);
Person p2("关羽", 27);
Person p3("张飞", 25);
Person p4("赵云", 21);
m.insert(pair<int, Person>(1, p1));
m.insert(pair<int, Person>(3, p3));
m.insert(pair<int, Person>(4, p4));
m.insert(pair<int, Person>(2, p2));
for (map<int, Person>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "姓名: " << it->second.m_Name << " 年龄: " << it->second.m_Age << endl;
}
}
int main() {
test01();
return 0;
}
********************输出结果************************
(base) BigfishdeMacBook-Pro:STL bigfish ./a.out
姓名: 刘备 年龄: 23
姓名: 关羽 年龄: 27
姓名: 张飞 年龄: 25
姓名: 赵云 年龄: 21
(base) BigfishdeMacBook-Pro:STL bigfish$ </pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="c++" cid="n87" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">#include <iostream>
include <set>
include <string>
include <map>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class MyCompare {
public:
bool operator()(const int & v1, const int & v2) const {
return v1 > v2;
}
};
void test01()
{
map<int, Person, MyCompare> m;
Person p1("刘备", 23);
Person p2("关羽", 27);
Person p3("张飞", 25);
Person p4("赵云", 21);
m.insert(pair<int, Person>(1, p1));
m.insert(pair<int, Person>(3, p3));
m.insert(pair<int, Person>(4, p4));
m.insert(pair<int, Person>(2, p2));
for (map<int, Person>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "姓名: " << it->second.m_Name << " 年龄: " << it->second.m_Age << endl;
}
}
int main() {
test01();
return 0;
}
********************输出结果*************************
(base) BigfishdeMacBook-Pro:STL bigfish ./a.out
姓名: 赵云 年龄: 21
姓名: 张飞 年龄: 25
姓名: 关羽 年龄: 27
姓名: 刘备 年龄: 23
(base) BigfishdeMacBook-Pro:STL bigfish$ </pre>
如果key值是自定义数据类型,那么必须通过仿函数来实现排序规则,且仿函数必须是常函数。
重点
这个错误在map自定义排序方法时需要使用const修饰词,但是在set的时候可以不用加,这个点还没搞清楚,如果那个大神指导可以留言指导一下。
注意:error: no matching function for call to object of type 'const MyCompare'
遇到这个错误时,需要在自定义比较函数后面加上const修饰词。关于函数后加const关键字可参考另一篇博文《C++核心——类中的const》
插入的时候按照刘备、张飞、赵云、关羽的顺序插入的,但是打印的顺序是赵云、张飞、关羽、刘备打印,我们可以通过仿函数,改变排序规则。
通过仿函数改变map的排序顺序
插入的时候按照刘备、张飞、赵云、关羽的顺序插入的,但是打印的顺序是刘备、关羽、张飞、赵云打印,所以默认是按照key的升序进行排序,我们可以通过仿函数,改变排序规则。
map容器默认通过key值进行升序排序
五、map的排序
上面的程序删除的是第一个位置的元素,因为迭代器是begin,也可以用end。不过end指向是最后一个元素的后面,所以删除最后一个元素m.erase(--m.end());
,需要向把跌抬起向前移动一位。如果需要制定其他位置的迭代器,可以使用advance()函数来实现,如下例子中,将it提升3位,那么it就事项第四个数。此时删除的就是赵云的数据。