27 lines
905 B
C++
27 lines
905 B
C++
|
#include <iostream>
|
||
|
#include <map>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
map<int, string> ms;
|
||
|
|
||
|
// 1. insert()函数
|
||
|
// map 在插入时会自动根据 map 的键来排序
|
||
|
ms.insert(pair<int, string>(1, "张三")); // pair<int, string>是map<int, string>::value_type的类型
|
||
|
ms.insert(pair<int, string>(4, "李四"));
|
||
|
ms.insert(make_pair(3, "王五")); // make_pair()函数可以自动推导出类型
|
||
|
ms.insert(map<int, string>::value_type(2, "赵六")); // value_type是map<int, string>的类型
|
||
|
ms[5] = "田七"; // 通过下标的方式插入数据
|
||
|
|
||
|
map<int, string>::iterator it = ms.begin();
|
||
|
for (; it != ms.end(); it++)
|
||
|
{
|
||
|
// cout << "sid = " << it->first << ",name = " << it->second << endl;
|
||
|
cout << "sid = " << (*it).first << ",name = " << (*it).second << endl;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|