#include"stdafx.h"
#include<map>
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
//STL中的map是采用二叉树实现的
map<char, string>mymap;
//添加元素
mymap['a'] = "linying";
mymap['b'] = "linyu";
mymap['c'] = "dale";
//遍历元素
map<char, string>::iterator it = mymap.begin();
//auto it = mymap.begin(); 使用自动推导类型 代码更加简洁
while (it!=mymap.end())
{
//string 要转换成char*
//first代表key second代表value
cout << it->first<<"=>"<<it->second.c_str() << endl;
it++;
}
//查找某个key是否存在
if (mymap.find('d')==mymap.end())
{
cout << "没有找到该key"<< endl;
}
getchar();
return 0;
}
STL map
最新推荐文章于 2024-06-01 15:38:13 发布