c++ 数据结构之STL之 map
2014-04-04 21:07
288 查看
标准模板库STL中的map
map与之前的set中内容大致相同,但是,保存的数据多了一项保存的数值型数据,即,map可以用一个专门的key来进行排序和搜索,而在Type中储存所需要保存的各类数据。对于map也有一个multimap,储存多个相同key值的数据。
stl中的所有模版都储存在std命名空间中。
#include <map>
using namespace std;
模板原型:
template < class Key, 关键字,用来比较,排序,搜索
class Type, 储存在map中的数值数据,保存各种数据。
class Traits = less<Key>, 关键字比较函数,可以重定义,定义方法与set类似
class Allocator=allocator<pair <const Key, Type> > >
储存管理设备,默认为allocator<pair<const Key,Type>>
由于map比set多了一个专门用来储存的Type,则其可储存更多东西,且可以使用默认的比较函数来进行判断,而专门的用一个函数来计算得到其关键字,更方便于比较,搜索。
begin(),
返回指向首元素的定位器iterator.
clera();清空所有元素。void
count(<template> Key):返回对应Key的元素个数
count(<template> Key):返回对应Key的元素个数,一般时0或1
empty():测试是否为空
end():返回最后一个元素的定位器
equal_range():返回一对定位器,一个指向>=
给定值的第一个Key,>给定值的第一个key。
erase();将一定范围内的元素删除,删除指定元素的方法:
map<int,string> a;
a.insert(pair<int,string>(compute("aab"),"aab"));
a.insert(pair<int,string>(compute("aca"),"caa"));
a.insert(pair<int,string>(compute("aaa"),"aaa"));
a.insert(pair<int,string>(compute("agg"),"baa"));
map<int,string>::iterator p;
p=a.find(compute("agg"));
a.erase(p);
find(template
Key):找出与给定key相同的定位器,若不存在,指向set的结束定位器。
get_allocator():返回set容器的一个allocator的副本
insert() 插入一个元素或一定数量的元素到应该的位置
map<int,string> a;
a.insert(pair<int,string>(compute("aab"),"aab"));
a.insert(pair<int,string>(compute("aca"),"caa"));
a.insert(pair<int,string>(compute("aaa"),"aaa"));
a.insert(pair<int,string>(compute("aab"),"baa"));
ower_bound() 返回指向大于(或等于)某值的第一个元素的迭代器
key_comp() 返回一个用于元素间值比较的函数
max_size() 返回集合能容纳的元素的最大限值
rbegin() 返回指向集合中最后一个元素的反向迭代器
rend() 返回指向集合中第一个元素的反向迭代器
size() 集合中元素的数目
swap() 交换两个指定对象的值。是指保存的数据交换,key保存不变
upper_bound() 返回大于某个值元素的迭代器
value_comp() 返回一个用于比较元素间的值的函数
operator[key] 寻找指定key的元素,并插入值,若未规定值,则值为默认值
map<int,string> a;
a.insert(pair<int,string>(compute("aab"),"aab"));
a.insert(pair<int,string>(compute("aca"),"caa"));
a.insert(pair<int,string>(compute("aaa"),"aaa"));
a.insert(pair<int,string>(compute("aab"),"baa"));
a[6]="awfawf";
a[2];
map与之前的set中内容大致相同,但是,保存的数据多了一项保存的数值型数据,即,map可以用一个专门的key来进行排序和搜索,而在Type中储存所需要保存的各类数据。对于map也有一个multimap,储存多个相同key值的数据。
stl中的所有模版都储存在std命名空间中。
#include <map>
using namespace std;
模板原型:
template < class Key, 关键字,用来比较,排序,搜索
class Type, 储存在map中的数值数据,保存各种数据。
class Traits = less<Key>, 关键字比较函数,可以重定义,定义方法与set类似
class Allocator=allocator<pair <const Key, Type> > >
储存管理设备,默认为allocator<pair<const Key,Type>>
由于map比set多了一个专门用来储存的Type,则其可储存更多东西,且可以使用默认的比较函数来进行判断,而专门的用一个函数来计算得到其关键字,更方便于比较,搜索。
begin(),
返回指向首元素的定位器iterator.
clera();清空所有元素。void
count(<template> Key):返回对应Key的元素个数
count(<template> Key):返回对应Key的元素个数,一般时0或1
empty():测试是否为空
end():返回最后一个元素的定位器
equal_range():返回一对定位器,一个指向>=
给定值的第一个Key,>给定值的第一个key。
erase();将一定范围内的元素删除,删除指定元素的方法:
map<int,string> a;
a.insert(pair<int,string>(compute("aab"),"aab"));
a.insert(pair<int,string>(compute("aca"),"caa"));
a.insert(pair<int,string>(compute("aaa"),"aaa"));
a.insert(pair<int,string>(compute("agg"),"baa"));
map<int,string>::iterator p;
p=a.find(compute("agg"));
a.erase(p);
find(template
Key):找出与给定key相同的定位器,若不存在,指向set的结束定位器。
get_allocator():返回set容器的一个allocator的副本
insert() 插入一个元素或一定数量的元素到应该的位置
map<int,string> a;
a.insert(pair<int,string>(compute("aab"),"aab"));
a.insert(pair<int,string>(compute("aca"),"caa"));
a.insert(pair<int,string>(compute("aaa"),"aaa"));
a.insert(pair<int,string>(compute("aab"),"baa"));
ower_bound() 返回指向大于(或等于)某值的第一个元素的迭代器
key_comp() 返回一个用于元素间值比较的函数
max_size() 返回集合能容纳的元素的最大限值
rbegin() 返回指向集合中最后一个元素的反向迭代器
rend() 返回指向集合中第一个元素的反向迭代器
size() 集合中元素的数目
swap() 交换两个指定对象的值。是指保存的数据交换,key保存不变
upper_bound() 返回大于某个值元素的迭代器
value_comp() 返回一个用于比较元素间的值的函数
operator[key] 寻找指定key的元素,并插入值,若未规定值,则值为默认值
map<int,string> a;
a.insert(pair<int,string>(compute("aab"),"aab"));
a.insert(pair<int,string>(compute("aca"),"caa"));
a.insert(pair<int,string>(compute("aaa"),"aaa"));
a.insert(pair<int,string>(compute("aab"),"baa"));
a[6]="awfawf";
a[2];
相关文章推荐
- C++数据结构与STL--栈的实现
- C++存储数据结构之map
- C++数据结构与STL--选择排序
- C++数据结构与STL--递归--汉诺塔
- C++存储数据结构之四map
- C++抽象编程——STL实战(5)——map在数据文件查找中的应用
- C++数据结构与STL--栈的应用--后缀表达式的计算
- C++ map,set内部数据结构
- C++数据结构与STL--双向循环链表(实现自定义iterator类)
- C++存储数据结构之四map
- C++存储数据结构之四map
- C++数据结构与STL--二分查找
- C++ map,set内部数据结构
- C++ map,set内部数据结构
- C++ STL的底层数据结构都是啥
- STL之map、set数据结构基础
- c++STL 数据结构之queue队列应用
- C++ map,set内部数据结构
- C++数据结构与STL--有序表,删除表重复项
- 数据结构_7:图算法 :c++/STL