您的位置:首页 > 其它

stl中map的使用

2016-03-28 00:00 176 查看
搞不懂他们为什么喜欢用pair刚刚在vs2010中测试了map。插入数据和查询数据的方法:

map<string,HWND> m;
HWND hh = NULL;
std::pair<string,HWND> p;
p.first = "a";
p.second = hh;
m["a"] = hh;
HWND hwnd = m["a"];
if (hwnd)
{
cout<<"hello world"<<std::endl;
}
//下面的方式明显简单多了
map<string,int> m2;
m2["a"] = 21;
int s = m2["a"];
map<int,string> m3;
m3[2] = "asd";
//遍历还是一样
map<string,int>::iterator   it=m2.begin();
for(;it!=m2.end();++it)
cout<<"key:"<<it->first
<<"value:"<<it->second<<std::endl;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: