C++ map容器

发布于:2024-03-29 ⋅ 阅读:(23) ⋅ 点赞:(0)

map/multimap容器

map基本概念

⭕map 是有序键值对容器,它的元素的键是唯一的。

⭕map中所有元素都是pair,pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值),所有元素都会根据元素的键值自动排序

⭕本质:map/multimap属于关联式容器,底层结构用二叉树(红黑树)实现。

优点:可以根据key值快速找到value值

map和multimap区别

  1. map不允许容器中有重复key值元素
  2. multimap允许容器中有重复key值元素
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
    map<string, int> my_map;
    my_map["apple"] = 1;
    my_map["banana"] = 2;
    my_map["orange"] = 3;
    // 插入一个已经存在的键值对,会覆盖原来的值
    my_map["apple"] = 4;
    // 输出map中的元素
    for (const auto& it : my_map) {
        cout << it.first << ": " << it.second << endl;
}

输出

  • #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    int main() {
        multimap<string, int> my_multimap;
        my_multimap.insert(make_pair("apple", 1));
        my_multimap.insert(make_pair("banana", 2));
        my_multimap.insert(make_pair("orange", 3));
        // 插入一个已经存在的键值对,不会覆盖原来的值
        my_multimap.insert(make_pair("apple", 4));
          
       // 输出multimap中的元素
        for (const auto& it : my_multimap) {
            cout << it.first << ": " << it.second << endl;
        }
    }

输出:

map构造和赋值

⭕功能描述:对map容器进行构造和赋值操作。

⭕函数原型:

map<type1name, type2name> maps;//第一个是键的类型,第二个是值的类型

构造一般是通过pair来实现

⭕赋值:

  • #include <iostream>
    #include <map>
    using namespace std;
    
    void PrintMap(map<int, int>& m) //使用迭代器打印
    {
           for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
           {
                   cout << "key=" << (*it).first << ' ' << "value=" << (*it).second << "   " ;
           }
           cout << endl;
    }
    
    void test01()
    {
           map<int, int> m1;
           m1.insert(pair<int, int>(1, 10));
           m1.insert(pair<int, int>(2, 20));
           m1.insert(pair<int, int>(3, 30));
           m1.insert(pair<int, int>(4, 40));
    
           PrintMap(m1);
    
           map<int, int>m2(m1); //m1赋值到m2
           PrintMap(m1);
    
           map<int, int>m3;
           m3 = m2;      //m2赋值到m3
           PrintMap(m3);
    }
    int main()
    {
           test01();
           return 0;
    }

输出:

⭕总结:map中所有元素都是成对出现,插入数据时要使用对组。

map大小和交换

⭕功能描述:统计map容器大小以及交换map容器

⭕函数原型:

size()           返回map中元素的个数

empty()          如果map为空则返回true

swap()            交换两个map

  • #include <iostream>
    #include <map>
    using namespace std;
    
    void PrintMap(map<int, int>& m)
    {
         for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
         {
             cout << "key=" << (*it).first << ' ' << "value=" << (*it).second << "   ";
         }
         cout << endl;
    }
    
    void test01()
    {
         map<int, int> m1; //创造一个键值为1,2,3,4的map容器
         m1.insert(pair<int, int>(1, 10));
         m1.insert(pair<int, int>(2, 20));
         m1.insert(pair<int, int>(3, 30));
         m1.insert(pair<int, int>(4, 40));
         PrintMap(m1);
    
         if (m1.empty()) //判断m1是否为空
         {
             printf("空\n");
         }
         else
         {
             printf("不空\n");
             cout<< "大小 = " << m1.size() << endl;
         }
    
         map<int, int> m2; //创造一个键值为10,20,30,40的map容器
         m2.insert(pair<int, int>(10, 1));
         m2.insert(pair<int, int>(20, 2));
         m2.insert(pair<int, int>(30, 3));
         m2.insert(pair<int, int>(40, 4));
    
         printf("交换前\n");
         cout << "m1: ";
         PrintMap(m1);
         cout << "m2: ";
         PrintMap(m2);
    
         printf("交换后\n");
    
         m1.swap(m2); //swap交换
         cout << "m1: ";
         PrintMap(m1);
         cout << "m2: ";
         PrintMap(m2);
    }
    
    int main()
    {
         test01();
         return 0;
    }

  • 输出:

总结:

  1. 统计大小——size
  2. 判断是否为空——empty
  3. 交换容器——swap

map插入和删除

⭕功能描述: map容器进行插入数据和删除数据

⭕函数原型:

  • #include <iostream>
    #include <map>
    using namespace std;
    
    void PrintMap(map<int, int>& m)
    {
           for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
           {
          //map可以使用it->first来访问键,it->second来访问值:
                   cout << "key=" << it->first << " " << "value=" << it->second << endl;
           }
           cout << endl;
    }
    void test01()
    {
           map<int, int>m1;
           //第一种
           m1.insert(pair<int, int>(1, 10));
           //第二种
           m1.insert(make_pair(2, 20));
           //第三种
           m1.insert(map<int, int>::value_type(3, 30));
           //第四种
           m1[4] = 40;
           cout << m1[4] << endl;
    
           PrintMap(m1);
    
           m1.erase(m1.begin());
           PrintMap(m1);
          
           m1[1] = 10;
           m1.erase(3); //按照key删除
           PrintMap(m1);
    
           m1.erase(m1.begin(), m1.end()); //全部删完了
           PrintMap(m1);
    
           m1.clear(); //清空m1
           PrintMap(m1);
    }
    int main()
    {
           test01();
           return 0;
    }

输出:

总结:map插入方式很多,记住其一即可

  1. 插入——insert
  2. 删除——erase
  3. 清空——clear

map查找和统计

⭕功能描述:对map容器进行查找和数据以及统计数据

⭕函数原型:

find()           查找一个元素(返回迭代器)

count()          返回指定元素出现的次数

  • #include <iostream>
    #include <map>
    using namespace std;
    
    void PrintMap(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>m1;
    
         m1.insert(pair<int, int>(1, 10));
         m1.insert(pair<int, int>(2, 20));
         m1.insert(pair<int, int>(3, 30));
    
         map<int, int>::iterator pos = m1.find(3);//返回迭代器
    
         if (pos != m1.end())
         {
             cout << "找到了" << ' ' << pos->first << " " << pos->second << endl;
         }
         else
         {
             cout << "没找到" << endl;
         }
         //map不允许插入重复key, 0 or 1
        
         //返回指定元素出现的次数,map只能是1,multimap可以大于1,可以重复
         int num = m1.count(3);
         cout << num << endl;
    }
    
    int main()
    {
         test01();
         return 0;
    }

输出:

总结:

  1. 查找——find(返回的是迭代器)
  2. 统计——cout(对于map,结果为0或1)

map容器排序

⭕学习目标:map容器默认排序为按照key值进行从小到大排序,掌握如何改变排序规则。map模板中的第三个参数可以指定排序规则。

⭕主要技术点:利用仿函数,可以改变排序规则。

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
/*
函数对象的概念
概念:
重载函数调用操作符的类,其对象常称为函数对象。例如上面代码中的MyCompare类
函数对象使用重载的()时,行为类似函数调用,也叫仿函数
本质:
函数对象(仿函数)是一个类,不是一个函数
特点:
函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
函数对象超出普通函数的概念,函数对象可以有自己的状态
函数对象可以作为参数传递
上述原文链接:https://blog.csdn.net/u011541946/article/details/108044498
*/
class MyCompare
{
public:
     bool operator()(int v1, int v2)const //重载()
     {
         return v1 > v2; //排倒序
     }
};

void PrintMap(map<int, int, MyCompare>& m)
{
     for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++)
     {
         cout << "key=" << it->first << " " << "value=" << it->second << endl;
     }
     cout << endl;
}

void test01()
{
     map<int, int, MyCompare>m1; //MyCompare类需要放入<>中
     m1.insert(make_pair(1, 10));
     m1.insert(make_pair(2, 20));
     m1.insert(make_pair(3, 30));
     m1.insert(make_pair(4, 40));
     m1.insert(make_pair(5, 50));

     PrintMap(m1);
}
int main(void)
{
     test01();
     return 0;
}

输出:

总结:

  1. 利用仿函数可以指定map容器的排序规则
  2. 对于自定义数据类型,map必须要指定排序规则,同set容器
本文含有隐藏内容,请 开通VIP 后查看