• STL中 Map 的基本用法


    一、大致了解

    map是STL中的一个关联容器,和python中的数据类型字典一样,map 类型变量中的元素也是由键-值对组成,没有重复的键。其底层实现是红黑树(非严格意义上的平衡二叉树)

    二、 基本用法

    基本用法包括:声明一个map类型的变量、向声明的map变量中插入元素、查找map变量中的元素、删除map变量中的元素、清空map变量中的元素等。

    2.1 声明一个map类型的变量

    先加入头文件 #include

    #include
    
    map 变量名;
    
    • 1
    • 2
    • 3

    2.2 向map变量中插入数据

    (1)用insert函数插入

    (2)用给Key赋value的方法插入

    #include
    #include
    using namespace std;
    
    int main()
    {
        map student;
        /*(1)*/
        student.insert(pair(001,"Zhang san"));
        student.insert(pair(002,"Li San"));
        /*(2)*/ 
        student[001] = "Zhang san";
        student[002] = "Li San";
    
        return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.3 查找map变量中的某个key

    (1) map变量.count(key) 只返回0或1,0表示map变量中不 包含key这个键,1则表示包括

    if(student.count(001) > 0){
        return student[001];
    }else{
        return NULL;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    (2) map变量.find(key) 返回一个迭代器,该迭代器指向查询到的这个key元素(存在key这个元素的时候)

    map ::iterator it;
    it = student.find(001);
    return it->second;
    
    • 1
    • 2
    • 3

    迭代器名称->first 表示迭代器当前指向的元素的key; 迭代器名称->second 表示迭代器当前指向的元素的value;

    2.4 删除map变量中的元素、清空map变量

    (1)删除元素用erase函数,删除成功返回1,否则返回0

    //配合使用迭代器删除
    it = student.find(001);
    int ans = student.erase(it);
    
    
    //直接删
    int ans = student.erase(001);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    (2) 清空map变量之间使用clear函数

    student.clear();
    
    • 1

    三、unordered_map与map的比较,make_pair与pair的比较

    1、unordered_map与map

    (1) 使用前需要引入的头文件不同,前者是 #include ,后者是 #include

    (2) 内部实现机理不同,前者是哈希表,后者是红黑树

    (3)map类型变量中元素是自动排序**,有序的,而unordered_map类型变量中的元素是无序的**

    2、make_pair()与pair()

    二者的用法示例:

    int main(int argc, char** argv)
    
    {
    
    	pair  product1 ("tomatoes",3.25);
    	pair  product2;
    	pair  product3;
    
    	product2.first ="lightbulbs"; // type of first is string
    	product2.second =0.99; // type of second is double
    	
    	product3 = make_pair ("shoes",20.0);
    	
    	cout <<"The price of "<< product1.first <<" is $"<< product1.second <<"
    ";
    	cout <<"The price of "<< product2.first <<" is $"<< product2.second <<"
    ";
    	cout <<"The price of "<< product3.first <<" is $"<< product3.second <<"
    ";
    
    	return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    【重铸Java根基】理解Java代理模式机制
    一文理解Hadoop分布式存储和计算框架入门基础
    区间DP day42
    做公众号1年啦
    简述直线模组的发展前景
    在 Python 中创建 Getter 和 Setter
    [C++11] --- 移动语义和完美转发
    制作rootfs镜像,通过fastboot烧录到x210开发板中验证
    正则系列之量词(Quantifiers)
    【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 连续区间和(100分) - 三语言AC题解(Python/Java/Cpp)
  • 原文地址:https://blog.csdn.net/web18484626332/article/details/126516654