C++中如何使用map
来源:爱站网时间:2019-12-07编辑:网友分享
你知道什么是map吗?大家在接触C++的时候是不是都有看到过map呢?那么你知道C++中如何使用map吗?相信很多人都是非常想知道的,那么接下来的内容中就让小编为大家介绍它的使用方法吧。
你知道什么是map吗?大家在接触C++的时候是不是都有看到过map呢?那么你知道C++中如何使用map吗?相信很多人都是非常想知道的,那么接下来的内容中就让小编为大家介绍它的使用方法吧。
/************************************************************************
*
* Map的特点: 1、存储Key-value对
* 2、支持快速查找,查找的复杂度基本是Log(N)
* 3、快速插入,快速删除,快速修改记
*
/************************************************************************/
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
map<const char*,int> m;
m["a"]=1;
m["b"]=6;
m["c"]=9;
map<const char*,int>::iterator it;
it=m.begin();
const char* c =it->first;
cout<<"first element is :"<<c<<endl;
int i = m["c"];
while(it!=m.end()){
cout << it->first<<";"<<it->second<<endl;
++it;
}
cout <<"m[\"c\"]="<<i<<endl;
cout <<"sizeof m:"<<m.size()<<endl;
cout <<"erase m[\"c\"](1:succ 0:failed):"<<m.erase("c")<<endl;
cout <<"erase m[\"c\"]:"<<m.erase("c")<<endl;
cout <<"sizeof m:"<<m.size()<<endl;
cout<<"m[c]="<<m["c"]<<endl;
cout<<"sizeof m :"<<m.size()<<endl;
return 0;
}
运行结果

以上就是小编为大家带来的C++中如何使用map的内容,想要学习的朋友们,赶快把这篇文章收入囊中吧,对你的学习是很有帮助的哦!
上一篇:带你了解C语言的指针
下一篇:关于c++的输入输出方法
