#include <iostream>
#include <map>
using namespace std;
int main()
{
//definition the map of mymap
std::map<char, int> mymap;
// 使用{}赋值是从c++11开始的,因此编译器版本过低时会报错,如visual studio 2012
/* map<int, string> ID_Name = {
{ 2015, "Jim" },
{ 2016, "Tom" },
{ 2017, "Bob" } };
*/
//three methods of insert data
mymap.insert(std::pair<char, int>('a', 100));
mymap.insert(std::pair<char, int>('z', 200));
mymap.insert(pair<char, int>('b', 300));
mymap['d']=400;
mymap.insert(map<char,int>::value_type('f',500) );
map<char,int>::iterator iter;
// check
for(iter=mymap.begin();iter !=mymap.end() ;iter++)
cout<<iter->first<<" "<<iter->second<<endl;
//delete one element
iter =mymap.find('b');
//mymap.erase(iter);
int n= mymap.erase('b');
cout<<"n:"<<n<<endl; //if delete b sucess ,return 1;else return 0
for(iter=mymap.begin();iter !=mymap.end() ;iter++)
cout<<iter->first<<" "<<iter->second<<endl;
//change
mymap['d']=4000;
cout<<mymap['d']<<endl;
// delete all element
mymap.erase( mymap.begin(), mymap.end() );
int nSize = mymap.size();
cout<<nSize;
return 0;
}
c++ map 增删查找四种基本操作
最新推荐文章于 2024-09-27 10:47:20 发布