/*
在头文件 #include<map> 中
键树是著名的数据结构 C++为我们提供了一个方便的容器
*/
#include<iostream>
#include<map>
using namespace std;
int main()
{
//声明(int为键,const char*为值)
map<int,const char*> m;
//插入元素
m.insert(make_pair(1,"ONE"));
m.insert(make_pair(10,"TEN"));
m[100] = "CHENZHANGJIE"; //其它写法
//查找元素
map<int, const char*>::iterator ite;
ite=m.find(1);
puts(ite->second); //输出ONE
ite=m.find(2);
if(ite==m.end()) puts("not found");
else puts(ite->second);
puts(m[10]); //其它写法
//删除元素
m.erase(10);
//遍历一遍所有元素
for(ite=m.begin(); ite!=m.end(); ++ite)
{
printf("%d: %s\n",ite->first,ite->second);
}
return 0;
}
C++键树容器map的调用
最新推荐文章于 2024-03-18 13:42:52 发布