std::map用法

本文详细介绍了C++ STL中的关联容器std::map,包括其构造、定义、基本操作函数以及方法,如插入元素、查找、删除等。map内部采用红黑树实现,自动排序,提供了高效的操作接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树AVL),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。

使用map对象首先要包括头文件,包含语句中必须加入如下包含声明:

#include <map>

using namespace map; 

1map介绍

1、1 map的构造

定义的原型为: Template<class T1, class T2>

map(); // 默认构造函数

map(const map& m) // 拷贝构造函数

map(iterator begin, iterator end ); //区间构造函数

map(iterator begin, iterator end, const traits& _compare) //带比较谓词的构造函数

map(iterator begin, iterator end, const traits& _compare, const allocator& all) //带分配器

1、2 map定义

1)map的基本定义

map对象是模板类,需要关键字和存储对象两个模板参数:

std:map<int, string> personnel;

这样就定义了一个用int作为索引,并拥有相关联的指向string的指针.

 

为了使用方便,可以对模板类进行一下类型定义:

typ

要将 `std::map<std::string, std::map<std::string, std::map<std::string, std::map<std::string, std::map<std::string, std::vector<CXHFCODE>>>>>>` 替换为使用 `CString` 作为键的结构,同时保持嵌套层次不变,需要对每一层的 `std::map<std::string, ...>` 进行替换。 具体做法是将所有以 `std::string` 为键的 `std::map` 替换为以 `CString` 为键的 `std::map`。由于 `CString` 是 MFC 中的字符串,其重载了比较运算符(如 `<`),因此可以直接用于 `std::map` 的键型而无需额外提供比较函数。 以下是替换前后的结构对比: 替换前: ```cpp std::map<std::string, std::map<std::string, std::map<std::string, std::map<std::string, std::map<std::string, std::vector<CXHFCODE>>>>>> ``` 替换后: ```cpp std::map<CString, std::map<CString, std::map<CString, std::map<CString, std::map<CString, std::vector<CXHFCODE>>>>>> ``` 遍历该结构时,需显式声明迭代器型并使用 `CString` 型的键进行访问: ```cpp typedef std::map<CString, std::map<CString, std::map<CString, std::map<CString, std::map<CString, std::vector<CXHFCODE>>>>>> NestedCStringMap; NestedCStringMap regions_; for (NestedCStringMap::iterator itProvince = regions_.begin(); itProvince != regions_.end(); ++itProvince) { const CString& provinceKey = itProvince->first; const std::map<CString, std::map<CString, std::map<CString, std::map<CString, std::vector<CXHFCODE>>>>>& cityMap = itProvince->second; for (std::map<CString, std::map<CString, std::map<CString, std::map<CString, std::vector<CXHFCODE>>>>>::iterator itCity = cityMap.begin(); itCity != cityMap.end(); ++itCity) { const CString& cityKey = itCity->first; const std::map<CString, std::map<CString, std::map<CString, std::vector<CXHFCODE>>>>& districtMap = itCity->second; for (std::map<CString, std::map<CString, std::map<CString, std::vector<CXHFCODE>>>>::iterator itDistrict = districtMap.begin(); itDistrict != districtMap.end(); ++itDistrict) { const CString& districtKey = itDistrict->first; const std::map<CString, std::map<CString, std::vector<CXHFCODE>>>& townMap = itDistrict->second; for (std::map<CString, std::map<CString, std::vector<CXHFCODE>>>::iterator itTown = townMap.begin(); itTown != townMap.end(); ++itTown) { const CString& townKey = itTown->first; const std::map<CString, std::vector<CXHFCODE>>& villageMap = itTown->second; for (std::map<CString, std::vector<CXHFCODE>>::iterator itVillage = villageMap.begin(); itVillage != villageMap.end(); ++itVillage) { const CString& villageKey = itVillage->first; const std::vector<CXHFCODE>& codes = itVillage->second; for (const CXHFCODE& code : codes) { // 执行 SQL 插入操作 } } } } } } ``` 上述代码通过逐层定义迭代器和引用型,确保在不使用 `auto` 的情况下正确访问每层数据[^3]。此方法保留了原始结构的嵌套逻辑,并利用 `CString` 提供的比较语义来保证 `std::map` 正常运行[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值