当前位置: 首页 > news >正文

[leetcode]map的用法

1. 定义和初始化

  • 定义std::map是一个关联容器,键值对会自动根据键的值进行排序(默认是升序)。

    cpp复制

    map<char, int> mp;
  • 插入元素:可以通过operator[]insert方法插入键值对。

    cpp复制

    mp['a'] = 1;
    mp['b'] = 3;
    mp['c'] = 2;

2. 遍历

  • 使用迭代器遍历std::map的迭代器可以用来遍历容器中的所有键值对。

    cpp复制

    map<char, int>::iterator it = mp.begin();
    map<char, int>::iterator itE = mp.end();
    while (it != itE) {
        cout << it->first << " ";  // 输出键
        cout << it->second << endl;  // 输出值
        it++;
    }

3. 查找

  • 使用find方法查找find方法会返回一个迭代器,指向找到的键值对。如果键不存在,则返回end()

    cpp复制

    map<char, int>::iterator itFind = mp.find('b');
    if (itFind != mp.end()) {
        cout << itFind->first << " " << itFind->second << endl;
    }

4. 删除

  • 删除指定迭代器的元素:可以通过erase方法删除指定迭代器指向的元素。

    cpp复制

    map<char, int>::iterator itB = mp.find('b');
    mp.erase(itB);
  • 删除指定键的元素:也可以通过键直接删除元素。

    cpp复制

    mp.erase('b');
  • 删除范围内的元素erase方法还可以删除指定范围内的元素。

    cpp复制

    mp.erase(it, itE);

5. 获取大小

  • 使用size方法size方法返回容器中元素的数量。

    cpp复制

    cout << "The size of map is: " << mp.size() << endl;

6. 清空

  • 使用clear方法clear方法会删除容器中的所有元素,但不会释放内存。

    cpp复制

    mp.clear();
    cout << "The size of map after cleaning is: " << mp.size() << endl;

7.插入

      使用insert方法insert插入键值对进入map容器。

8.总结

std::map是一个非常强大的关联容器,适用于需要快速查找、插入和删除键值对的场景。它的主要特点包括:

  • 自动排序:键值对会根据键自动排序。

  • 高效查找:查找操作的时间复杂度为O(logn)。

  • 灵活操作:支持插入、删除、查找等多种操作

Coding

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<char, int> mp;
    mp['b'] = 3;
    mp['a'] = 1;
    mp['c'] = 2;
    //map will automatic sort by key value
    map<char,int>::iterator it = mp.begin();
    map<char,int>::iterator itE = mp.end();

    while (it != itE) {
        cout << it->first << " ";  
        cout << it->second << endl;
        it++;
    }
    
    //The map can use so-called find function
    map<char,int>::iterator itFind = mp.find('b');
    cout <<endl << itFind->first << " " << itFind->second << endl;
    
    
    //map can erase the aimed element or a contiuous part of elements
    //erase iterator b
    map<char,int>::iterator itB = mp.find('b');
    mp.erase(itB);
    
    //erase function can erase a contious elements
    //mp.erase(it,itE);
    
    //erase function can remove the elemnt by relative key
    //mp.erase('b');
    
    //size function can get the size of map
    cout << "The size of map is :" << " " << mp.size() << endl;
    
    //clear function can remove all of the map
    mp.clear();
    cout << "The size of map after cleaning is :" << mp.size() << endl;
    
    return 0;
}

相关文章:

  • SpringBoot-配置文件中敏感信息的加密保姆级教程
  • Solr-搜索引擎-入门到精通
  • Ubuntu与Windows之间相互复制粘贴的方法
  • Spring MVC 请求与响应
  • Node.js下载安装配置指南(精简)
  • 给Web开发者的HarmonyOS指南02-布局样式
  • AndroidTV 直播电视-v20250323-第三方电视直播APP
  • 【设计模式】深入解析装饰器模式(Decorator Pattern)
  • SpringBoot通过Map实现天然的策略模式
  • 一文解读DeepSeek的安全风险、挑战与应对策略
  • 蓝桥杯2022年第十三届决赛真题-最大数字
  • Springboot 学习 之 Shardingsphere 按照日期水平分表(一)
  • Swift 二分法求函数的近似解
  • Ubuntu20.04系统安装IsaacSim4.5与IsaacLab环境
  • html和css 实现元素顺时针旋转效果(椭圆形旋转轨迹)
  • MongoDB 的索引是提高查询性能的核心机制,类似于传统关系型数据库的索引。以下是对 MongoDB 索引的详细说明:
  • 基础实验2-2.1 整数的分类处理
  • openresty-nginx添加新模块
  • 机器学习中的 K-均值聚类算法及其优缺点
  • 什么是Dify,以及我们能用它来做什么
  • wordpress 仿站步骤/seowhy官网
  • 视频聊天室网站开发/阿里妈妈推广网站
  • 平湖网站建设/网络运营怎么学
  • 信息网站建设预算/资源猫
  • 网站建设7/seo优化排名技术百度教程
  • 做ptt网站/成都全网营销推广