STL-map/multimap容器

原文链接: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 bigfishg++ 07_mapTest.cpp (base) BigfishdeMacBook-Pro:STL bigfish ./a.out
    姓名: 赵云 年龄: 21
    姓名: 刘备 年龄: 23
    姓名: 张飞 年龄: 25
    姓名: 关羽 年龄: 27
    (base) BigfishdeMacBook-Pro:STL bigfish$</pre>

    1. 在类里重载小于号< 注意只重载小于号,不要去重载大于号,因为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 bigfishg++ 07_mapTest.cpp (base) BigfishdeMacBook-Pro:STL bigfish ./a.out
      姓名: 赵云 年龄: 21
      姓名: 刘备 年龄: 23
      姓名: 张飞 年龄: 25
      姓名: 关羽 年龄: 27
      (base) BigfishdeMacBook-Pro:STL bigfish$</pre>

      如果需要更加严谨就应该在重载方法中进一步判断,因为这样如果某个属性等的话,就只能插入一个进去,因为map的key不能重复,如果是multimap就不用担心这点。

    2. 利用仿函数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./a.out 姓名: 刘备 年龄: 23 姓名: 关羽 年龄: 27 姓名: 张飞 年龄: 25 姓名: 赵云 年龄: 21 删除后: 姓名: 关羽 年龄: 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="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 bigfishg++ 07_mapTest.cpp (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 bigfishg++ 07_mapTest.cpp (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就事项第四个数。此时删除的就是赵云的数据。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容