STL教程(八): 关联容器--multimap/map

 一、multimap/map简介

template<
    class Key,
    class T,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<std::pair<const Key, T> >
> class map;

参数介绍:

        Key:map的键的数据类型

        T:map的值的数据类型

       Compare: 键比较函数,一个二元谓词,接受两个T类型参数并返回一个bool,默认为less<Key> 即递增

         Allocator :用于定义存储分配模型的分配器对象的类型。默认使用分配器类模板,它定义了最简单的内存分配模型

multimap的声明与map完全相同,这里就不再累述了。

multimap/map是一个关联容器,其底层实现通常是红黑树(二叉搜索树),与multiset/set一样,但与multiset/set容器不同的是,multimap/map容器内的元素的值和键不是同一个值,在map中,每个键值都是唯一的,不可能重复,但是在multimap中的键值却是可以重复的。

它们的排序是通过比较函数(一元谓词)Compare 进行排序的,默认为less<T> (升序)。

multimap/mapt的搜索、删除和插入操作非常快,时间复杂度为logn。

二、multimap/map的成员函数

multimap的成员函数与map完全相同,下面以map为例进行介绍:

1、构造函数

(1)构造一个空的map容器,没有元素,也是默认构造函数

explicit map(const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());

explicit map (const allocator_type& alloc);

(2)构造一个包含了范围[first,last)元素的set容器,每个元素都从该范围内的相应元素以相同的顺序就地构造。

template <class InputIterator>
  map(InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());


(3) 以map容器x作为数据源,构造一个新map容器,其中新set容器中的元素来自于x中元素拷贝的副本。

map(const map& x);
map(const map& x, const allocator_type& alloc);

(4)构造一个容器,并直接从x中移动元素,之后x中的元素将被移除

map(map&& x);
map(map&& x, const allocator_type& alloc);

(5)以相同的顺序构造一个map容器,其中包含l中每个元素拷贝的副本

map(initializer_list<value_type> l,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());

2、operator=函数

(1)使用map列表other对其进行复制拷贝

map& operator=( const map& other );

(2)使用map列表other对其进行移动拷贝,之后other的数据将被清空

map& operator=( map&& other );

(3)使用初始化列表对map进行赋值

map& operator=( std::initializer_list<T> l );

3、迭代器

iterator begin();  // 返回容器的迭代器到第一个元素
const_iterator cbegin(); // 返回容器的迭代器到第一个元素
iterator end();    // 返回容器的迭代器到最后一个元素 
const_iterator cend();   // 返回容器的迭代器到最后一个元素
reverse_iterator rbegin(); // 返回容器的反向迭代器到第一个元素
const_reverse_iterator crbegin();// 返回容器的反向迭代器到第一个元素
reverse_iterator rend();   // 返回容器的反向迭代器到最后一个元素
const_reverse_iterator crend();  // 返回容器的反向迭代器到最后一个元素

4、容量相关

bool empty(); // 判断容器是否为空
size_type size();// 返回容器中当前元素个数
size_type max_size(); //返回容器内可存放的最大元素个数

5、元素访问

/*
 返回容器中键为k的元素的value的引用。若容器中不存在该元素,则使用值的默认构造函数进行构造后再返回,且此时容器大小+1
*/
mapped_type& operator[] (const key_type& k);

// 返回容器中键为k的元素的值的引用。若容器中不存在该元素,该函数将抛出一个out_of_range异常
mapped_type& at (const key_type& k);
const mapped_type& at (const key_type& k) const;

6、修改容器

/* 向容器内插入一个value_type(std::pair<Key,Value>)类型元素,返回一个pair,其成员pair::first设置为一个迭代器,指向新插入的元素或已存在在map中的元素。如果插入成功,则pair::second元素为true,若该元素已存在,则为false */
pair<iterator,bool> insert (const std::pair<Key,Value>& val);
pair<iterator,bool> insert (std::pair<Key,Value>&& val);

// 提示新元素val应该在position位置插入,但仅仅是提示,并返回新元素位置或在map中已经存在元素位置 
iterator insert (const_iterator position, const std::pair<Key,Value>& val);
iterator insert (const_iterator position, std::pair<Key,Value>&& val);

// 插入新元素[first,last)
template <class InputIterator>
  void insert (InputIterator first, InputIterator last);

// 将初始化列表l内的元素插入到容器中
void insert (initializer_list<std::pair<Key,Value>> l);

// 删除位于position的元素,并返回position之后的迭代器
iterator erase (const_iterator position);

// 删除键key的元素,并返回删除元素的总个数
size_type erase (const key_type& k);

// 删除位置为[first,last)的元素,并返回最后一个移除元素之后的迭代器
iterator  erase (const_iterator first, const_iterator last);

// 用x的内容交换容器的内容
void swap (map& x);

// 清空容器内所有元素
void clear() ;

/* 以args为参数就地构造新元素,并将其插入到容器中。则其成员pair::first设置为一个迭代器,指向新插入的元素或已存在在set 中的元素。如果插入成功,则pair::second元素为true,若该元素已存在,则为false */
pair<iterator,bool> emplace (Args&&... args);

/* 其作用同emplace,但该函数使用position参数作为提示,指示该元素的可能位置,将极大的加快了插入过程 */
iterator emplace_hint (const_iterator position, Args&&... args);

6、查找相关

// 返回容器中元素的键值为key的元素个数 
size_type count( const Key& key ) const

// 查找容器元素中键值为key的元素,并返回其迭代器位置
iterator find( const Key& key );
const_iterator find( const Key& key ) const;

// 查找容器中符合特定要求的键值,并返回两个迭代器:一个指向不小于key的第一个元素,一个指向大于key的第一个元素
std:: pair < iterator,iterator > equal_range ( const Key & key ) ;
std:: pair < const_iterator,const_iterator > equal_range ( const Key & key ) const ;

// 查找容器中键值不小于(即大于或等于)key 的第一个元素的迭代器
iterator lower_bound( const Key& key );
const_iterator lower_bound( const Key& key ) const;

// 查找容器中键值大于key 的第一个元素的迭代器
iterator upper_bound( const Key& key );
const_iterator upper_bound( const Key& key ) const;

7、比较函数 

// 返回比较key的函数对象,它是容器构造时的参数comp的副本,并且因为set的key与value完全相同,所以它与value_comp也相同
key_compare key_comp() const;

// 返回比较value的函数对象,它是容器构造时的参数comp的副本,并且因为set的key与value完全相同,所以它与value_comp也相同
value_compare value_comp()const;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chiang木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值