[STL] map
来源:互联网 发布:网络电视看不见翡翠台 编辑:程序博客网 时间:2024/06/10 16:19
[STL] map
- STL map
- 属性
- 定义并赋值
- 查询
- 删除
- 其他函数
- STL map
属性
- 声明形式
map<Key, Data, Compare, Alloc>//第三个是比较函数,第四个是分配器。
- 头文件
#include<map>
- 迭代
for(map<string, int>::iterator p = f.begin(); p != f.end(); p ++) cout << p -> first << '=' << p -> second << endl;
map内置元素是pair类型。
- 比较运算符
对于 STL 中的 map,我们经常使用时,key_type 经常是基本类型,因此编译器自带比较函数可以比较元素大小,从而建立红黑树,从map的头开始顺序地读取数据,其数据的顺序将不同于你插入时候的顺序。
比如<string, int> f
依次输入如下关系并插入
a 5e 1c 3d 2b 2
最后迭代的顺序会是
a=5b=2c=3d=2e=1
定义并赋值
- 构造函数
map<string, int> s;//普通定义map<string, int> p(s);//拷贝s中的关系map<string, int> p(s.begin(), s.end()) //区间构造函数
- 插入关系
数组插入
s["hello"] = 7;
insert函数
s.insert(make_pair("hello", 7));
s.insert(map<string, int>::value_type("hello", 7));
数组插入比较慢。
查询
- 下标查询
printf("%d", s["hello"]);
如果没有对应的键值会返回一个默认键。(int为0,string为空字符串)
- find函数
map<string, int>::iterator q = s.find("hello");if(q == s.end())printf("NONE");else cout << s -> first << '=' << s -> second << endl;
删除
- erase函数
s.erase(s.begin());//删除单个关系s.erase(s.begin(), s.end());//删除区间内的关系s.erase("hello");//删除单个关系
- clear函数
s.clear() = s.erase(s.begin(), s.end());
其他函数
- begin()
头部迭代器 - end()
尾部迭代器 - empty()
为空返回真 - key_comp()
返回key键值的比较函数 - swap()
交换两个map - max_size()
返回可以容纳的最大元素个数
0 0
- STL-map
- stl -map
- stl map
- STL Map
- STL---map
- STL(MAP)
- STL(map)
- STL--map
- STL map
- stl map
- STL map
- STL----map
- stl map
- STL map
- 【STL】map
- STL--map
- STL map
- STL map
- POJ-1543
- 混合高斯模型深入理解和分析
- NSOperationQueue 线程池的使用
- 生产者/消费者模式
- Java实训初体验
- [STL] map
- 解决NAT模式,主机ping不通虚拟机
- springMVC+mybatis框架搭建
- CodeVS2599 电路的稳定性
- STL学习之map
- k个最大的m段子数组和
- Tomcat默认访问路径的配置
- Iterator抽取迭代基类/Memento抽取备忘管理类/Interpreter终结非终结解释
- 线程之间的通信