map
1 生成构造
#include <iostream>
#include <map>
bool fncomp (char lhs, char rhs) {return lhs<rhs;}
struct classcomp {
bool operator() (const char& lhs, const char& rhs) const
{return lhs<rhs;}
};
int main ()
{
std::map<char,int> first;
first['a']=10;
first['b']=30;
first['c']=50;
first['d']=70;
std::map<char,int> second (first.begin(),first.end());
std::map<char,int> third (second);
std::map<char,int,classcomp> fourth;
bool(*fn_pt)(char,char) = fncomp;
std::map<char,int,bool(*)(char,char)> fifth (fn_pt);
std::map<char,int> first;
std::map<char,int> second;
first['x']=8;
first['y']=16;
first['z']=32;
second=first;
first=std::map<char,int>();
return 0;
}
2 操作
2.1 大小和容量
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
mymap['a']=10;
mymap['b']=30;
mymap['c']=50;
mymap['d']=70;
mymap.size();
mymap.max_size();
mymap.empty();
for(auto it=mymap.cbegin();it!=mymap.cend();++it)
std::cout<<it->first<<"=>"<<it->second<<" \n";
return 0;
}
2.2 比较
2.3 find
2.4 截取部分
2.5 增,插,删,改
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
mymap['a']=10;
mymap['b']=30;
mymap['c']=50;
mymap['d']=70;
mymap.insert ( std::pair<char,int>('a',100) );
mymap.insert ( std::pair<char,int>('z',200) );
std::map<char,int>::iterator it = mymap.begin();
mymap.insert (it, std::pair<char,int>('b',300));
mymap.insert (it, std::pair<char,int>('c',400));
std::map<char,int> anothermap;
anothermap.insert(mymap.begin(),mymap.find('c'));
it=mymap.find('b');
mymap.erase (it);
mymap.erase ('c');
it=mymap.find ('e');
mymap.erase ( it, mymap.end() );
mymap.clear();
return 0;
}
3.6 access element
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
mymap['a']=10;
mymap['b']=30;
mymap['c']=50;
mymap['d']=70;
std::cout<<mymap.at('a');
std::cout<<mymap['b'];
return 0;
}
2.7其他
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
std::pair<std::map<char,int>::iterator,std::map<char,int>::iterator> ret;
ret = mymap.equal_range('b');
std::cout << "lower bound points to: ";
std::cout << ret.first->first << " => " << ret.first->second << '\n';
std::cout << "upper bound points to: ";
std::cout << ret.second->first << " => " << ret.second->second << '\n';
return 0;
}
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
std::map<char,int>::iterator itlow,itup;
mymap['a']=20;
mymap['b']=40;
mymap['c']=60;
mymap['d']=80;
mymap['e']=100;
itlow=mymap.lower_bound ('b');
itup=mymap.upper_bound ('d');
mymap.erase(itlow,itup);
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
return 0;
}
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
char c;
mymap ['a']=101;
mymap ['c']=202;
mymap ['f']=303;
for (c='a'; c<'h'; c++)
{
std::cout << c;
if (mymap.count(c)>0)
std::cout << " is an element of mymap.\n";
else
std::cout << " is not an element of mymap.\n";
}
return 0;
}
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
std::map<char,int>::iterator it;
mymap['a']=50;
mymap['b']=100;
mymap['c']=150;
mymap['d']=200;
it = mymap.find('b');
if (it != mymap.end())
mymap.erase (it);
std::cout << "elements in mymap:" << '\n';
std::cout << "a => " << mymap.find('a')->second << '\n';
std::cout << "c => " << mymap.find('c')->second << '\n';
std::cout << "d => " << mymap.find('d')->second << '\n';
return 0;
}