博客
关于我
c++标准库中的关联容器
阅读量:271 次
发布时间:2019-03-01

本文共 1178 字,大约阅读时间需要 3 分钟。

C++标准库中有序和无序的容器

C++标准库中提供了多种容器,用于存储键值对或单一键的数据。这些容器可以根据键的存储顺序分为有序和无序两种类型。

有序的容器包括:

  • map:用于存储键值对(key: value),并且键是按一定顺序排列的。
  • set:用于存储单一键,键是按一定顺序排列的。
  • multimap:与map类似,但允许键的重复出现。
  • multiset:与set类似,但允许键的重复出现。
  • 无序的容器包括:

  • unordered_map:与map类似,但键是无序存储的。
  • unordered_set:与set类似,但键是无序存储的。
  • unordered_multimap:与multimap类似,但键是无序存储的。
  • unordered_multiset:与multiset类似,但键是无序存储的。
  • 默认分配器的设置

    • map的默认分配器是std::allocator<std::pair<const K, V>>
    • set的默认分配器是std::allocator<K>

    主要操作

  • 添加元素

    • map需要使用std::pair类型来添加元素。例如:
    std::map
    map2;map2.insert(std::make_pair(2, "xxx"));

    或者可以使用括号初始化的方式:

    map2.insert({4, "xxxx"});
  • 遍历元素

    • 使用迭代器来遍历元素。例如:
    std::map
    map2;map2.insert(std::make_pair(2, 6.66));map2.insert({4, 8.88});for (auto it = map2.begin(); it != map2.end(); ++it) { qDebug() << it->first << it->second;}
  • 删除元素

    • 使用erase()方法删除元素。例如:
    std::map
    map2;map2.insert(std::make_pair("www", 6.66));map2.insert({"qqqqq", 8.88});// 遍历并打印元素for (auto it = map2.begin(); it != map2.end(); ++it) { qDebug() << it->first << it->second;}map2.erase("www");// 再次遍历打印元素for (auto it = map2.begin(); it != map2.end(); ++it) { qDebug() << it->first << it->second;}
  • 这些容器在实际应用中广泛使用,适用于根据键进行快速查找和排序操作的场景。

    转载地址:http://tavx.baihongyu.com/

    你可能感兴趣的文章
    OAuth2.0_授权服务配置_密码模式及其他模式_Spring Security OAuth2.0认证授权---springcloud工作笔记145
    查看>>
    OAuth2.0_授权服务配置_资源服务测试_Spring Security OAuth2.0认证授权---springcloud工作笔记146
    查看>>
    OAuth2.0_环境介绍_授权服务和资源服务_Spring Security OAuth2.0认证授权---springcloud工作笔记138
    查看>>
    OAuth2.0_环境搭建_Spring Security OAuth2.0认证授权---springcloud工作笔记139
    查看>>
    oauth2.0协议介绍,核心概念和角色,工作流程,概念和用途
    查看>>
    OAuth2授权码模式详细流程(一)——站在OAuth2设计者的角度来理解code
    查看>>
    oauth2登录认证之SpringSecurity源码分析
    查看>>
    OAuth2:项目演示-模拟微信授权登录京东
    查看>>
    OA系统多少钱?OA办公系统中的价格选型
    查看>>
    OA系统选型:选择好的工作流引擎
    查看>>
    OA让企业业务流程管理科学有“据”
    查看>>
    OA项目之我的会议(会议排座&送审)
    查看>>
    OA项目之我的会议(查询)
    查看>>
    Object c将一个double值转换为时间格式
    查看>>
    object detection之Win10配置
    查看>>
    object detection训练自己数据
    查看>>
    object detection错误Message type "object_detection.protos.SsdFeatureExtractor" has no field named "bat
    查看>>
    object detection错误之Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
    查看>>
    object detection错误之no module named nets
    查看>>
    Object of type 'ndarray' is not JSON serializable
    查看>>