map详解
来源:互联网 发布:php入门书籍 知乎 编辑:程序博客网 时间:2024/06/05 10:48
map详解
一、clear(),empty(),size().insert(),begin(),end()
clear() 清空迭代器
begin() 返回指向第一个元素额迭代器
end() 返回指向末尾元素的迭代器
empty() 如果为空,返回 true
size() 返回元素的数量
insert(pair<keytype,valuetype> val) 插入 pair 类型元素,对 map 返回一个 pair,first 指向插入元素的迭代器, second 表示插入是否成功
insert(loc,pair<keytype, valuetype>val) 从 loc 寻找一个可以插入值为 value 的元素的位置并将其插入返回 map
#include<iostream>#include<map>using namespace std;int main(){ map<int,char> mymap; map<int,char>::iterator it; mymap.insert(pair<int,char>(1,'a')); mymap.insert(pair<int,char>(4,'b')); pair<map<int,char>::iterator,bool> ret; ret = mymap.insert ( pair<int,char>(4,'c')); if (ret.second==false) { cout << "element 'z' already existed"; cout << " with a value of " << ret.first->second <<endl; } cout<<"mymap1:"<<endl; for(it=mymap.begin();it!=mymap.end();it++) cout<<it->first<<" => "<<it->second<<endl; mymap.insert(it, pair<char,int>(2,'c')); mymap.insert(it, pair<char,int>(3,'d')); cout<<"mymap2:"<<endl; for(it=mymap.begin();it!=mymap.end();it++) cout<<it->first<<" => "<<it->second<<endl; cout<<"size:"<<mymap.size()<<endl; mymap.clear(); if(mymap.empty()) cout<<"clear"<<endl; return 0;}代码运行如下:
二、rbegin(),rend()
rbegin() 返回逆向迭代器,指向链表末尾
rend() 返回指向开头之前位置的迭代器
#include<iostream>#include<map>using namespace std;int main(){ map<int,char> mymap; map<int,char>::reverse_iterator its; mymap[1] = 'a'; mymap[2] = 'b'; mymap[3] = 'c'; cout<<"mymap:"<<endl; for(its=mymap.rbegin(); its!=mymap.rend(); ++its) std::cout << its->first << " => " << its->second <<endl; return 0;}代码运行如下:
三、erase()
erase(loc) 删除 loc 所指元素
erase(start,end) 删除[start,end)之间的元素
erase(key_type key) 删除 key 值为 value 的元素,并返回 删除的个数
#include<iostream>#include<map>using namespace std;int main (){ map<int,char> mymap; map<int,char>::iterator it; map<int,char>::iterator its; mymap[1]='a'; mymap[2]='b'; mymap[3]='c'; mymap[4]='d'; mymap[5]='e'; mymap[6]='f'; cout<<"first:"<<endl; for(its=mymap.begin(); its!=mymap.end(); ++its) cout << its->first << " => " << its->second <<endl; it=mymap.find(2); mymap.erase (it); mymap.erase (3); it=mymap.find (5); cout<<"second:"<<endl; for(its=mymap.begin(); its!=mymap.end(); ++its) cout << its->first << " => " << its->second <<endl; mymap.erase (it,mymap.end()); cout<<"3rd:"<<endl; for(its=mymap.begin(); its!=mymap.end(); ++its) cout << its->first << " => " << its->second <<endl; return 0;}代码运行如下:
四、find()
find(key_type key) 返回一个迭代器指向键值为 key 的元 素,未找到返回 end()
#include<iostream>#include<map>using namespace std;int main (){ map<int,char> mymap; map<int,char>::iterator it; mymap[1]='a'; mymap[2]='b'; mymap[3]='c'; mymap[4]='d'; it=mymap.find(2); mymap.erase(it); mymap.erase(mymap.find(4)); cout<< "mymap:" <<endl; cout<< "1 => " << mymap.find(1)->second <<endl; cout<< "3 => " << mymap.find(3)->second <<endl; return 0;}代码运行如下:
五、其他
lower_bound(key_type key) 返回一个迭代器指向>=key 的第一个元素
upper_bound(key_type key) 返回一个迭代器,指向>key 的第一个元素
#include<iostream>#include<map>using namespace std;int main (){ map<int,char> mymap; map<int,char>::iterator itlow,itup,it; mymap[1]='a'; mymap[2]='b'; mymap[3]='c'; mymap[4]='d'; mymap[5]='e'; itlow=mymap.lower_bound(2); itup=mymap.upper_bound(4); mymap.erase(itlow,itup); cout<<"mymap:"<<endl; for(it=mymap.begin(); it!=mymap.end(); ++it) cout << it->first << " => " << it->second <<endl; return 0;}代码运行如下:
本文下载地址:http://pan.baidu.com/s/1dD7nPVN
0 0
- Map详解
- map详解
- map详解
- map详解
- Map详解
- map详解
- Map.Entry 遍历map详解
- Map.Entry和Map中的map.keySet()、map.entrySet()详解
- System.map详解
- map-STL用法详解
- map用法详解
- map - STL 用法详解
- STL MAP用法详解
- STL Map详解
- STL map使用详解
- System.map文件详解
- NAT以及MAP详解
- STL map用法详解
- Windows平台Android下2.1版cocos2d-x的环境搭建
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState
- android平板分辨率
- OSGi的几个命令
- DLNA中的UPnP技术浅析
- map详解
- dts文件解析
- HDU 1032 The 3n + 1 problem
- pjsua帮助手册(中文)
- USBKey scsi
- Android 4.0 Contacts 首页(PeopleActivity)界面研究
- 关于thread(C++版)
- 练习,计算公式
- ‘svn’不是内部或外部命令,也不是可运行的程序