STL-map

map内部是用红黑树维持的有序结构。定义:map<int,string>mapStudent;

查找的时间复杂度为对数级别.

1.构造方法学习两种:第一种:用insert函数插入pair数据,mapStudent.insert(pair<int, string>(0,"jiangjing"));第二种:用数组方式插入数据

mapStudent[1] = "jiangjing1";  mapStudent[2] =  "jiangjing2";

2.遍历也学习两种:第一种:用迭代器遍历map<int, string>::iterator iter;

for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
cout<<iter->first<<" " <<iter->second<<endl;

第二种:用数组遍历

for(int nIndex = 0; nIndex < nSize; nIndex++)
cout<<mapStudent[nIndex]<<endl;

3.用count函数来判定关键字是否出现,出现返回1,没出现返回0;用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,程序说明:

iter = mapStudent.find(1);

if(iter != mapStudent.end())
cout<<"Find, the value is "<<iter->second<<endl;
else
cout<<"Do not Find"<<endl;

4.数据的删除用erase(key);函数

未完待续。。。。。

原文地址:https://www.cnblogs.com/jiangjing/p/3934464.html