C mapmap 是 C 标准库中的关联容器存储 键值对key-value并按照键自动排序一、基本特点1.头文件#include2.存储方式键值对 pairconst Key, Value3.排序按键升序默认4.唯一性键唯一不能重复5.时间复杂度:插入/删除/查找 O(log n)二、常用操作1. 声明和初始化#includemap#includestringusingnamespacestd;// 空 mapmapint,stringm1;// 初始化列表mapint,stringm2{{1,apple},{2,banana},{3,cherry}};// 拷贝构造mapint,stringm3(m2);2. 插入元素mapint,stringm;// 方法1使用 insertm.insert({1,apple});m.insert(pairint,string(2,banana));m.insert(make_pair(3,cherry));// 方法2使用 [] 运算符如果键不存在则创建m[4]date;// 插入键4值datem[1]avocado;// 键1已存在更新值// 方法3insert_or_assignm.insert_or_assign(5,elderberry);3. 访问元素mapint,stringm{{1,apple},{2,banana},{3,cherry}};// 使用 []键不存在时会创建危险string s1m[1];// applestring s2m[100];// 错误会插入键100值为空字符串// 使用 at()推荐键不存在时抛异常string s3m.at(2);// banana// string s4 m.at(100); // 抛出 out_of_range 异常// 使用 find最安全autoitm.find(3);if(it!m.end()){string sit-second;// cherry}4. 删除元素mapint,stringm{{1,a},{2,b},{3,c},{4,d}};// 按键删除m.erase(2);// 删除键2// 按迭代器删除autoitm.find(3);if(it!m.end()){m.erase(it);}// 删除范围autofirstm.find(1);autolastm.find(4);m.erase(first,last);// 删除 1 到 4 之前的元素5. 查找元素mapint,stringm{{1,apple},{2,banana},{3,cherry}};// find返回迭代器找不到返回 end()autoitm.find(2);if(it!m.end()){coutit-first - it-secondendl;}// count返回键存在的数量0 或 1if(m.count(2)){cout键2存在endl;}// lower_bound / upper_bound用于范围查询autolowm.lower_bound(2);// 第一个 2 的位置autoupm.upper_bound(2);// 第一个 2 的位置6. 遍历mapint,stringm{{1,a},{2,b},{3,c}};// 范围 for 循环for(constautop:m){coutp.first - p.secondendl;}// 迭代器for(autoitm.begin();it!m.end();it){coutit-first - it-secondendl;}三、完整示例#includeiostream#includemap#includestringusingnamespacestd;intmain(){mapstring,intscores;// 插入scores[Alice]95;scores[Bob]87;scores.insert({Charlie,92});// 修改scores[Alice]98;// 查找string nameBob;if(scores.find(name)!scores.end()){coutname 的分数是 scores[name]endl;}// 遍历自动按名字排序for(constauto[name,score]:scores){coutname - scoreendl;}// 删除scores.erase(Charlie);cout总人数: scores.size()endl;return0;}
C++ map详解
发布时间:2026/5/21 20:41:40
C mapmap 是 C 标准库中的关联容器存储 键值对key-value并按照键自动排序一、基本特点1.头文件#include2.存储方式键值对 pairconst Key, Value3.排序按键升序默认4.唯一性键唯一不能重复5.时间复杂度:插入/删除/查找 O(log n)二、常用操作1. 声明和初始化#includemap#includestringusingnamespacestd;// 空 mapmapint,stringm1;// 初始化列表mapint,stringm2{{1,apple},{2,banana},{3,cherry}};// 拷贝构造mapint,stringm3(m2);2. 插入元素mapint,stringm;// 方法1使用 insertm.insert({1,apple});m.insert(pairint,string(2,banana));m.insert(make_pair(3,cherry));// 方法2使用 [] 运算符如果键不存在则创建m[4]date;// 插入键4值datem[1]avocado;// 键1已存在更新值// 方法3insert_or_assignm.insert_or_assign(5,elderberry);3. 访问元素mapint,stringm{{1,apple},{2,banana},{3,cherry}};// 使用 []键不存在时会创建危险string s1m[1];// applestring s2m[100];// 错误会插入键100值为空字符串// 使用 at()推荐键不存在时抛异常string s3m.at(2);// banana// string s4 m.at(100); // 抛出 out_of_range 异常// 使用 find最安全autoitm.find(3);if(it!m.end()){string sit-second;// cherry}4. 删除元素mapint,stringm{{1,a},{2,b},{3,c},{4,d}};// 按键删除m.erase(2);// 删除键2// 按迭代器删除autoitm.find(3);if(it!m.end()){m.erase(it);}// 删除范围autofirstm.find(1);autolastm.find(4);m.erase(first,last);// 删除 1 到 4 之前的元素5. 查找元素mapint,stringm{{1,apple},{2,banana},{3,cherry}};// find返回迭代器找不到返回 end()autoitm.find(2);if(it!m.end()){coutit-first - it-secondendl;}// count返回键存在的数量0 或 1if(m.count(2)){cout键2存在endl;}// lower_bound / upper_bound用于范围查询autolowm.lower_bound(2);// 第一个 2 的位置autoupm.upper_bound(2);// 第一个 2 的位置6. 遍历mapint,stringm{{1,a},{2,b},{3,c}};// 范围 for 循环for(constautop:m){coutp.first - p.secondendl;}// 迭代器for(autoitm.begin();it!m.end();it){coutit-first - it-secondendl;}三、完整示例#includeiostream#includemap#includestringusingnamespacestd;intmain(){mapstring,intscores;// 插入scores[Alice]95;scores[Bob]87;scores.insert({Charlie,92});// 修改scores[Alice]98;// 查找string nameBob;if(scores.find(name)!scores.end()){coutname 的分数是 scores[name]endl;}// 遍历自动按名字排序for(constauto[name,score]:scores){coutname - scoreendl;}// 删除scores.erase(Charlie);cout总人数: scores.size()endl;return0;}