首页 > 电脑

想问一下c++中的map的作用是什么?看到了map

更新时间2018-04-07 11:21:52

网上都是一些简要的介绍,没有说map<char*是什么意思,我对c++不太了解,只是想知道这这个的用法

C++的map是一种数据结构

它可以存储二维的数组(你可以认为,它可以存储一张表)

且它数组的成员类型可以自定义的

如以下人员的年龄表:

姓名   年龄      

张三   22      

李四   20      

王五   23      

就可以用map实现

#include <string>
#include <map>
#include <iostream>
using namespace std;
typedef map<string, int, less<string> > info_type;

template <class First, class Second>
ostream& operator<<(ostream& out,
                   const pair<First,Second> & p)
{
cout << p.first << " 的年龄为 " << p.second << " 岁";
return out;
}

int main(void)
{

info_type info;
typedef info_type::value_type value_type;

info.insert(value_type(string("张三"),   22));
info.insert(value_type(string("李四"),   20));
info.insert(value_type(string("王五"),   23));

info_type::iterator p = info.begin();
while (p != info.end())
 cout  << *p++ << endl;

return 0;
}


上一篇:我1050的显卡为什么带不动吃鸡

下一篇:java属性封装的问题