/*
* modify element
* insert erase clear swap
*/#include <iostream>#include <map>int main ()
{
std::map<char,int> mymap;
mymap['a']=10;
mymap['b']=30;
mymap['c']=50;
mymap['d']=70;
// first insert function version (single parameter):
mymap.insert ( std::pair<char,int>('a',100) );
mymap.insert ( std::pair<char,int>('z',200) );
// second insert function version (with hint position):std::map<char,int>::iterator it = mymap.begin();
mymap.insert (it, std::pair<char,int>('b',300)); // max efficiency inserting
mymap.insert (it, std::pair<char,int>('c',400)); // no max efficiency inserting// third insert function version (range insertion):std::map<char,int> anothermap;
anothermap.insert(mymap.begin(),mymap.find('c'));
it=mymap.find('b');
mymap.erase (it); // erasing by iterator
mymap.erase ('c'); // erasing by key
it=mymap.find ('e');
mymap.erase ( it, mymap.end() ); // erasing by range
mymap.clear();
//mymap.swap(mymap2)return0;
}
3.6 access element
/*
* access element
* operator[] at
*/#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'];
return0;
}
2.7其他
//范围// map::equal_range#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';
/*
* output:
* 下限指向:'b'=> 20
*上限指向:'c'=> 3
*/return0;
}
// map::lower_bound/upper_bound#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'); // itlow points to b
itup=mymap.upper_bound ('d'); // itup points to e (not d!)
mymap.erase(itlow,itup); // erases [itlow,itup)// print content:for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
return0;
}
//数量// map::count#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";
elsestd::cout << " is not an element of mymap.\n";
}
return0;
}
// map::find#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);
// print content: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';
return0;
}