c++ map 记录

map
map是一种关系型容器,必须包含两种元素,一个是关键字,另一个是值。map有两种,一种是一般的map对象,它是一对一对应关系;另外一种是multimap,这中类型是可以允许一对多的关系。这两中map类型除此之外没有其他的区别。

头文件: <map>

map的定义:
map <type, type> map对象名
或者是typedef map <type, type> 新的map对象类型名 (这里假设是MyMap)
typedef map<int, string> MyMap;
MyMap myMap;

map的增添操作:(前三种返回的是一个pair <iterator, bool>,不能重复插入相同的值,第四种是直接将原位置的值覆盖)
方法一:
myMap.insert(pair <int, "string">(1, "google1"));  (1)   my_Map["a"]   =   1; 
myMap.insert(pair<int, "string">(2, "google2"));
方法二:
myMap.insert(make_pair <int, string> (3,"google3"));
myMap.insert(make_pair <int, string> (4,"google4"));
方法三:
myMap.insert(map <int, string>::value_type(5, "google5"));
myMap.insert(map <int, string>::value_type(6, "google6"));
方法四:
myMap[7] ="google7";

map的遍历:
for (map<int, string>::iterator it = myMap.begin(); it != myMap.end(); ++it){
cout << it->first << " "<< it->second;

map的删除:while(){
map<int, string>::iterator it = myMap.begin();
cout << it->first << " " << it-> second << endl;
myMap.erase(it);
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值