• map的使用方法


    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。
    下面举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述(本篇文章中不用char *来描述字符串,而是采用STL中string来描述),下面给出map描述代码:
    一、插入数据

    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int main()
    {
    
          mapm;
          m.insert(pair(1,'a'));
          m.insert(pair(2,'b'));
          m.insert(pair(3,'c'));
          map::iterator it;
          for(it=m.begin();it!=m.end();it++)
          {
              cout<second<
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    另一种方法
    这种方法会覆盖原值,此代码的运行结果为:
    2
    3

    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int main()
    {
        map mapStudent;
        mapStudent[1] = 1;
        mapStudent[1] = 2;  //可覆盖原值
        mapStudent[2] = 3;
        map::iterator  iter;
        for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
        {
            cout<<" "<second<
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    二、map的大小
    在往map里面插入了数据,我们怎么知道当前已经插入了多少数据呢,可以用size函数,用法如下:
    Int nSize = mapStudent.size();
    三、数据的遍历
    以下代码的运行结果为
    3 student_three
    2 student_two
    1 student_one

    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int main()
    {
    
       map mapStudent;
        mapStudent.insert(pair(1, "student_one"));
        mapStudent.insert(pair(2, "student_two"));
        mapStudent.insert(pair(3, "student_three"));
        map::reverse_iterator  iter;
        for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
        {
            cout<first<<" "<second<

四、数据的查找(包括判定这个关键字是否在map中出现)
1.用count函数,但是该函数的返回值只有0和1,1为找到,0为没有找到,不能返回要查找的值
2.用find函数
以下代码的运行结果为
find!student_one

#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{

   map m;
    m.insert(pair(1, "student_one"));
    m.insert(pair(2, "student_two"));
    m.insert(pair(3, "student_three"));
    map::iterator  it;
    it=m.find(1);
    if(it!=m.end())
        cout<<"find!"<second;
    else
        cout<<"not find!";
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

五、map的清空
清空用map.clear()函数

 m.erase(m.begin(),m.end());//删除的是一个前闭后开的集合
  • 1

六、检查map是否为空
用函数map.empty()
返回值为true时表示map为空
七、数据的删除

#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
    map m;
    m.insert(pair(1, "student_one"));
    m.insert(pair(2, "student_two"));
    m.insert(pair(3, "student_three"));
    //如果要删除1,用迭代器删除
    map::iterator iter;
    iter = m.find(1);
    m.erase(iter);

    for(iter = m.begin(); iter != m.end(); iter++)
    {
        cout<first<<" "<second<

八、排序
对结构体的排序

#include
#include
#include
#include
#include
#include
using namespace std;

typedef struct tagStudentInfo
{
    int      nID;
    string   strName;
    bool operator < (tagStudentInfo const& _A) const
    {
        //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序
        if(nID < _A.nID)  return true;
        if(nID == _A.nID) return strName.compare(_A.strName) < 0;
        return false;
    }
}StudentInfo, *PStudentInfo;  //学生信息

int main()
{
    int nSize;
    //用学生信息映射分数
    mapmapStudent;
    map::iterator iter;
    StudentInfo studentInfo;
    studentInfo.nID = 1;
    studentInfo.strName = "student_one";
    mapStudent.insert(pair(studentInfo, 90));
    studentInfo.nID = 2;
    studentInfo.strName = "student_two";
    mapStudent.insert(pair(studentInfo, 91));

    for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)
        cout<first.nID<first.strName<second<
  • 相关阅读:
    文件格式转换
    东北大学acm暑期夏令营结构体
    GEE:LST地表温度反演函数
    基于YOLOv8模型和UA-DETRAC数据集的车辆目标检测系统(PyTorch+Pyside6+YOLOv8模型)
    java字符串(String & StringBuilder)
    【卫朋】智能硬件 | 做好一款电子硬件产品,工具必不可少
    牛客网《剑指offer》专栏刷题练习之双指针算法的使用
    【支持M1】MacDroid for Mac:Mac和Android安卓设备数据互通
    [可视化] 点云可视化工具open3d的使用
    mPEG-Cholesterol,mPEG-CLS,甲氧基-聚乙二醇-胆固醇可用于脂质体制备
  • 原文地址:https://blog.csdn.net/m0_67392811/article/details/126435023