c++ stl: map

map :映射 ,key、value。(key是唯一的)

//头文件
#include <map>

//定义
map <string,int> mp;
mp["goodplus"] = 99;
mp["lx"] = 100;
cout<<mp["goodplus"]<<endl;//输出 key 为goodplus的value (99)

//遍历输出
map <string,int> :: iterator it;//定义迭代器
it = mp.begin();//初始化
while (it != mp.end()){
    cout<<it ->first <<" "<< it ->second;//first -> key; second -> value
    it ++;//"++"不要忘记哦
}

//常用方法
//find方法
map <string,int> :: iterator it2 = mp.find("goodplus");//参数为value,返回类型为迭代器
cout<<"key 为 goodplus 的值为" << it2 ->second;
//erase方法
mp.erase(it2);//参数要删除元素的迭代器
//size()方法
cout<<mp.size()<<endl;//元素的个数
//clear()方法
mp.clear();//清除所有元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值