C++中map提供的是一种键值对容器,每一对中的第一个值称之为键(key),每个关键字只能在map中出现一次;第二个称之为该键的对应值。使用map容器需要包含头文件#include
#include "iostream"
using namespace std;
#include "map"
#include "string"
//map元素的添加/遍历/删除基本操作
void rng01()
{
map<int, string> map1;//键值对需要包含两个类型
//四种插值方法
//1.第一种插值法(利用pair<第一种类型,第二种类型>(第一个参数,第二个参数))
map1.insert(pair<int, string>(1, "demo1"));
map1.insert(pair<int, string>(2, "demo2"));
//2.第二种插值法(利用make_pair(第一个参数,第二个参数))
map1.insert(make_pair(3, "demo3"));
map1.insert(make_pair(4, "demo4"));
//3.第三种插值法(利用map<第一种类型,第二种类型>::value_type(第一个参数,第二个参数))
map1.insert(map<int, string>::value_type(5, "demo5"));
map1.insert(map<int, string>::value_type(6, "demo6"));
//4 第四种插值法(利用:集合名[key]=value)
map1[7] = "demo7";
map1[8] = "demo8";
//map集合遍历
for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++)
{
cout << it->first << "\t" << it->second << endl;
//it是指向m