🏠map类
map介绍:¶
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的。map类的学习可以参考资料。
优点及功能:¶
- 自动建立key-value(下表-值)的对应。key和value可以是任意需要的类型。
- 根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次;1,000,000个记录,最多查找20次。
- 快速插入Key-Value 记录。
- 快速删除记录
- 根据Key修改value记录。
- 遍历所有记录。
声明:¶
头文件如下:
#include<map>
声明map需要关键字和存储对象两个模板参数:
std::map<int,string> m;
其中int为索引,string为存储对象
插入数据:¶
- 用insert函数插入pair数据:
#include <map> #include <string> #include <iostream> using namespace std; int main() { map<int, string> mapStudent; mapStudent.insert(pair<int, string>(1, "student_one")); mapStudent.insert(pair<int, string>(2, "student_two")); mapStudent.insert(pair<int, string>(3, "student_three")); map<int, string>::iterator iter; for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) cout<<iter->first<<' '<<iter->second<<endl; return 0; }
To be continued…