std::map的一些用法

本文深入探讨了C++中map容器的使用方法,包括下标法、std::pair插入、自定义排序及高效遍历等关键特性,并通过实例展示了如何灵活运用这些特性来解决实际问题。

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

一. 下标法的使用

所谓下标法, 就是类似数组那样子, 而在std:::map中, 下标就是你定义的key.

例子:

#include <map>
#include <string>

struct STTT
{
	int nI;
};

typedef std::map<std::string, STTT*> StringSTTTPMap;
typedef StringSTTTPMap::iterator  StringSTTTPMapIterator;


StringSTTTPMap mm;

STTT* p1 = new STTT;
p1->nI = 1;
STTT* p2 = new STTT;
p2->nI = 2;

mm["222"] = p1;
mm["111"] = p2;

// 这个用法很好用, 可以在
// "如果已经存在, 则不能改变; 如果不存在,则加入"的
// 场景下使用
if(0 == mm["333"])
{
	mm["333"] = new STTT;
}

这个例子利用了map的一些特性:

1. 列用map, 对key排序.

2. 下标法使用时, 如果该下标不存在, 则map会自动添加, 这种情况特别适合在在value是指针的情况下使用, 因为如果是值的话, 涉及到对象的拷贝, 而指针的话就只是指针的拷贝而已, 当然, 你需要管理好你的指针.(我很喜欢这样用)


二. std::pair, insert, 遍历map中所有数据

#include <map>
#include <string>

struct STTT
{
	int nI;
};
typedef std::map<std::string, STTT*> StringSTTTPMap;
typedef StringSTTTPMap::iterator  StringSTTTPMapIterator;

StringSTTTPMap mm;

STTT* p1 = new STTT;
p1->nI = 1;
STTT* p2 = new STTT;
p2->nI = 2;

// insert插入, 插入的是std::pair
mm.insert(std::pair<std::string, STTT*>("222", p1));
mm.insert(std::pair<std::string, STTT*>("111", p2));

StringSTTTPMapIterator  iter;
for(iter = mm.begin(); iter != mm.end(); iter++)
{
	// pair的first和second
	std::cout << iter->first << ”   ” << iter->second << std::end;
}

三. 自定义排序

你可以自定义key的排序,  暂时没验证.

要将 `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、付费专栏及课程。

余额充值