map详解

map详解

一、clear(),empty(),size().insert(),begin(),end()

 clear() 清空迭代器 

begin() 返回指向第一个元素额迭代器 

end() 返回指向末尾元素的迭代器 

empty() 如果为空,返回 true 

size() 返回元素的数量 

insert(pair<keytype,valuetype> val)  插入 pair 类型元素,对 map 返回一个 pairfirst 指向插入元素的迭代器, 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值