数据结构
Map既映射,其中所有的元素都是pair且有序,同时拥有实值(value)和关键字(key)。Map以RB-tree底层机制,其实就是一种平衡二叉搜索树。
为了保护map内元素的组织有序性,故C++不允许用户对map元素的key值随意修改,只能对value进行修改。
pair的定义:
template <class T1, class T2>
struct pair
{
typedef T1 first_type;
typedef T2 second_type;
T1 first; //都是public
T2 second;
pair() : first(T1()), second(T2()) {}
pair(const T1 &a, const T2 &b) : first(a), second(b) {}
};
成员定义
//缺省采用递增排序
template <class Key, class T, class Compare = less<Key>, class Alloc = alloc>
class map
{
public:
// typedefs:
typedef Key key_type; // 键值类型
typedef T data_type; // 实值,数据类型
typedef T mapped_type; //
typedef pair<const Key, T> value_type; // 元素类型(键值/真值)
typedef Compare key_compare; // 键值的比较函数
private:
//以map类型(一个pair)的第一个类型作为TB-tree的键值类型.
//所以在RB-tree中,键值key不能修改,但是数值data可以修改。
typedef rb_tree<key_type, value_type,
select1st<value_type>, key_compare, Alloc>
rep_type;
rep_type t; // 红黑树作为底层数据结构
构造函数
map() : t(Compare()) {}
explicit map(const Compare& comp) : t(comp) {} //explicit抑制构造函数的隐式转换
//STL
template <class InputIterator>
map(InputIterator first, InputIterator last) : t(Compare())
template <class InputIterator>
map(InputIterator first, InputIterator last, const Compare& comp) : t(comp)
//C++
map(const value_type* first, const value_type* last) : t(Compare())
map(const value_type* first, const value_type* last, const Compare& comp) : t(comp)
map(const_iterator first, const_iterator last) : t(Compare())
map(const_iterator first, const_iterator last, const Compare& comp) : t(comp)
//复制构造函数
map(const map<Key, T, Compare, Alloc>& x) : t(x.t) {}
map<Key, T, Compare, Alloc>& operator=(const map<Key, T, Compare, Alloc>& x)
接口
key_compare key_comp() const { return t.key_comp(); }
value_compare value_comp() const { return value_compare(t.key_comp()); }
正向迭代器:
iterator begin()
const_iterator begin() const
iterator end()
const_iteratorend() const
反向迭代器:
reverse_iterator rbegin()
const_reverse_iterator rbegin() const
reverse_iterator rend()
const_reverse_iterator rend() const
计数:
size_type size() const
size_type max_size() const
//重载[],返回的是引用
T &operator[](const key_type &k)
Swap:
voidswap(map<Key, T, Compare, Alloc> &x){ t.swap(x.t);}
插入/删除
//单个插入
pair<iterator, bool> insert(const value_type &x) { return t.insert_unique(x); }
iterator insert(iterator position, const value_type &x)
//插入区间
template <classInputIterator>
voidinsert(InputIterator first, InputIterator last)
voidinsert(const value_type *first, const value_type *last)
voidinsert(const_iterator first, const_iterator last)
voiderase(iterator position)
size_type erase(const key_type &x) { return t.erase(x); }
voiderase(iterator first, iterator last){ t.erase(first, last);}
void clear() { t.clear(); }
操作
查找:
iterator find(const key_type &x) { return t.find(x); }
const_iterator find(const key_type &x) const { return t.find(x); }
统计键值为x的个数:
size_type count(const key_type &x) const { return t.count(x); }
求区间://[x,x)区间
const_iteratorlower_bound(const key_type &x)
const_iteratorupper_bound(const key_type &x)
const_iteratorequal_range(const key_type &x)
比较:
friendbooloperator==__STL_NULL_TMPL_ARGS(const map &,const map&);
friendbooloperator<__STL_NULL_TMPL_ARGS(const map &,const map&);