#include <iostream>
#include <map>
using namespace std;
bool funcomp(const char&first,const char &second){return first<second;}
class classcomp
{
public:
bool operator()(const char&first,const char&second){return first<second;}
};
int main()
{
//begin
cout<<"--begin"<<endl;
map<char,int>beginmap;
map<char,int>::iterator beginit;
beginmap['a']=100;
beginmap['b']=200;
beginmap['c']=300;
for(beginit=beginmap.begin();
beginit!=beginmap.end();++beginit)
cout<<(*beginit).first<<"=>"<<(*beginit).second<<endl;
map<char,int>emptymap;
beginmap.swap(emptymap);
beginmap.clear();
//clear
cout<<"--clear"<<endl;
map<char,int>clearmap;
map<char,int>::iterator clearit;
clearmap['a']=100;
clearmap['b']=200;
clearmap['c']=300;
cout<<"clearmap contains:"<<endl;
for(clearit=clearmap.begin();
clearit!=clearmap.end();++clearit)
cout<<(*clearit).first<<"=>"<<(*clearit).second<<endl;
clearmap.clear();
clearmap['x']=111;
clearmap['y']=222;
cout<<"clearmap contains:"<<endl;
for(clearit=clearmap.begin();
clearit!=clearmap.end();++clearit)
cout<<(*clearit).first<<"=>"<<(*clearit).second<<endl;
clearmap.swap(emptymap);
clearmap.clear();
//count
cout<<"--cout"<<endl;
map<char,int>countmap;
countmap['a']=100;
countmap['b']=200;
countmap['d']=300;
char c;
for(c='a';c<'h';c++)
{
cout<<c;
if(countmap.count(c)>0)
cout<<"is an element of countmap."<<endl;
else
cout<<"is not an element of countmap."<<endl;
}
countmap.swap(emptymap);
countmap.clear();
//equal_range
cout<<"--equal_range"<<endl;
map<char,int> equal_rangemap;
pair<map<char,int>::iterator,map<char,int>::iterator> retpair;
equal_rangemap['a']=100;
equal_rangemap['b']=200;
equal_rangemap['c']=300;
retpair=equal_rangemap.equal_range('b') ;
cout<<"lower bound points to:"<<retpair.first->first<<"=>"<<retpair.first->second<<endl;
cout<<"upper bound points to:"<<retpair.second->first<<"=>"<<retpair.second->second<<endl;
equal_rangemap.swap(emptymap);
equal_rangemap.clear();
//erase
cout<<"--erase"<<endl;
map<char,int>erasemap;
map<char,int>::iterator eraseit;
erasemap['a']=100;
erasemap['b']=200;
erasemap['c']=300;
erasemap['d']=400;
erasemap['e']=500;
erasemap['f']=600;
eraseit=erasemap.find('b');
erasemap.erase(eraseit);//erase by it
erasemap.erase('c');//erase by key
eraseit=erasemap.find('e');
erasemap.erase(eraseit,erasemap.end());//erase by range
cout<<"earsemap:"<<endl;
for(eraseit=erasemap.begin();
eraseit!=erasemap.end();++eraseit)
cout<<(*eraseit).first<<"=>"<<(*eraseit).second<<endl;
//find
cout<<"--find"<<endl;
map<char,int>findmap;
map<char,int>::iterator findit;
findmap['a']=100;
findmap['b']=200;
findmap['c']=300;
findmap['d']=400;
findit=findmap.find('a');
findmap.erase(findmap.find('b'));
cout<<"c=>"<<findmap.find('c')->second<<endl;
cout<<"d=>"<<findmap.find('d')->second<<endl;
//insert
cout<<"--insert"<<endl;
map<char,int>insertmap;
map<char,int>::iterator insertit;
pair<map<char,int>::iterator,bool> insertpair;
insertmap.insert(pair<char,int>('a',100));
insertmap.insert(make_pair('b',200));
insertmap.insert(map<char,int>::value_type('c',300));
insertpair=insertmap.insert(pair<char,int>('c',300));
if(insertpair.second==false)
cout<<"element 'c' is already existed with a value of "<<
insertpair.first->second<<endl;
insertit=insertmap.begin();
insertmap.insert(pair<char,int>('d',400));
insertmap.insert(pair<char,int>('e',500));
map<char,int>insertmap2;
//insert it range
insertmap2.insert(insertmap.begin(),insertmap.find('c'));
cout<<"insertmap:"<<endl;
for(insertit=insertmap.begin();
insertit!=insertmap.end();++insertit)
cout<<(*insertit).first<<"=>"<<(*insertit).second<<endl;
cout<<"insertmap2:"<<endl;
for(insertit=insertmap2.begin();
insertit!=insertmap2.end();++insertit)
cout<<(*insertit).first<<"=>"<<(*insertit).second<<endl;
//bound
cout<<"--bound"<<endl;
map<char,int>boundmap;
map<char,int>::iterator boundit,bounditlow,bounditup;
boundmap['a']=100;
boundmap['b']=2;
boundmap['c']=6;
boundmap['d']=88;
boundmap['e']=99;
bounditlow=boundmap.lower_bound('b');//'b'<=? b
bounditup=boundmap.upper_bound('b');//'b'<? c
cout<<"bound:"<<endl;
cout<<(*bounditlow).first<<"=>"<<(*bounditlow).second<<endl;
cout<<(*bounditup).first<<"=>"<<(*bounditup).second<<endl;
//erase [b,c)
boundmap.erase(bounditlow,bounditup);
cout<<"boundmap:"<<endl;
for(boundit=boundmap.begin();
boundit!=boundmap.end();++boundit)
cout<<(*boundit).first<<"=>"<<(*boundit).second<<endl;
//map
cout<<"--map"<<endl;
map<char,int> testmap;
testmap['a']=100;
testmap['b']=200;
testmap['c']=300;
testmap['d']=400;
map<char,int>testmap2(testmap.begin(),testmap.end());
map<char,int>testmap3(testmap2);
map<char,int,classcomp> testmap4;
bool (*fn_t)(const char&,const char&)=funcomp;
map<char,int,bool (*)(const char&,const char&)>testmap5(fn_t);
testmap.swap(emptymap);
testmap.clear();
testmap2.swap(emptymap);
testmap2.clear();
testmap3.swap(emptymap);
testmap3.clear();
// testmap4.swap(emptymap);
// testmap4.clear();
// testmap5.swap(emptymap);
// testmap5.clear();
testmap4=map<char,int,classcomp>();
testmap4.clear();
testmap5=map<char,int,bool (*)(const char&,const char&)>();
testmap5.clear();
//=
cout<<"--="<<endl;
map<char,int>opmap1,opmap2;
opmap1['a']=100;
opmap1['b']=200;
opmap1['c']=300;
opmap2=opmap1;
opmap1=map<char,int>();
cout<<"opmap1 size:"<<opmap1.size()<<endl;
cout<<"opmap2 size:"<<opmap2.size()<<endl;
//[]
cout<<"--[]"<<endl;
map<char,string>testmap11;
testmap11['a']="aaa";
testmap11['b']="bbb";
testmap11['c']=testmap11['b'];
testmap11['d']=testmap11['e'];
cout<<"map a:"<<testmap11['a']<<endl;
cout<<"map b:"<<testmap11['b']<<endl;
cout<<"map c:"<<testmap11['c']<<endl;
cout<<"map d:"<<testmap11['d']<<endl;
cout<<"map e:"<<testmap11['e']<<endl;
// cout<<"map f:"<<testmap11["f"]<<endl;
map<char,string>testmap11_emptymap;
testmap11.swap(testmap11_emptymap);
testmap11.clear();
//rbegin
map<char,int>rbeginmap;
map<char,int>::reverse_iterator rbeginit;
rbeginmap['a']=100;
rbeginmap['b']=200;
rbeginmap['c']=300;
for(rbeginit=rbeginmap.rbegin();
rbeginit!=rbeginmap.rend();
++rbeginit)
cout<<"rbeginmap: "<<rbeginit->first<<"=> "<<rbeginit->second<<endl;
rbeginmap.swap(emptymap);
rbeginmap.clear();
//swap
cout<<"--swap"<<endl;
map<char,int>swapmap1,swapmap2;
map<char,int>::iterator swapmapit;
swapmap1['a']=100;
swapmap1['b']=200;
swapmap1['c']=300;
swapmap2['qw']=99;
swapmap2['er']=88;
swapmap1.swap(swapmap2);
for(swapmapit=swapmap1.begin();
swapmapit!=swapmap1.end();
++swapmapit)
cout<<"swapmap1: "<<swapmapit->first<<"=>"<<swapmapit->second<<endl;
for(swapmapit=swapmap2.begin();
swapmapit!=swapmap2.end();
++swapmapit)
cout<<"swapmap2: "<<swapmapit->first<<"=>"<<swapmapit->second<<endl;
swapmap1.swap(emptymap);
swapmap1.clear();
swapmap2.swap(emptymap);
swapmap2.clear();
//
return 0;
}
map公共函数
最新推荐文章于 2023-06-20 20:05:01 发布