C++进阶 —— C++11新增容器

目录

一,array

二,forward_list

三,unordered

unordered_set

unordered_multiset

unordered_map

unordered_multimap


静态数组array、forward_list、unordered系列;

一,array

      array是固定大小尺寸的序列容器;其内部,不存储除元素本身外的任何其他数据;不像其他容器,不通过分配器allocator管理元素,也无法动态的调整大小;零大小的数组也是有效的,但不应被引用;交换两个数组容器是线性操作;

//迭代器
begin(),end()
rbegin(),rend()
cbegin(),cend()
crbegin(),crend()

//容量
size()
empty()
//元素访问
reference operator[] (size_type n);
reference at ( size_type n );
reference front();
reference back();
value_type* data() noexcept;
//修改
void fill (const value_type& val);
void swap (array& x) noexcept(noexcept(swap(declval<value_type&>(),declval<value_type&>())));
//非成员函数
template <size_t I, class T, size_t N> T& get (array<T,N>& arr) noexcept;
template <size_t I, class T, size_t N> T&& get (array<T,N>&& arr) noexcept;
template <size_t I, class T, size_t N> const T& get (const array<T,N>& arr) noexcept;

二,forward_list

         该类容器是以单链表实现的,能以O(1)快速在任意位置插入或删除操作;与list容器相比,此容器为单链只能单方向迭代,且效率较高;与其他标准容器(array、vector、deque)相比,其在插入、提取、移动任意位置元素表现更好,如排序;但缺点是不可直接通过位置访问,需从头迭代;考虑到效率,此容器没有容量size成员函数,获取大小size可通过begin、end;

构造函数

其他成员函数

//迭代器
before_begin()
begin(),end()
cbefore_begin()
cbegin(),cend()

empty()
front()
swap ();
resize ();
clear();
//修改
void assign (InputIterator first, InputIterator last);
void emplace_front (Args&&... args);
iterator emplace_after (const_iterator position, Args&&... args);

void push_front (const value_type& val);
void pop_front();

iterator insert_after ( const_iterator position, const value_type& val );
iterator erase_after (const_iterator position);
//操作
void splice_after (const_iterator position, forward_list& fwdlst);
void remove (const value_type& val);
void remove_if (Predicate pred);
void unique();	
void merge (forward_list& fwdlst);
void sort();
void reverse() noexcept;

三,unordered

  • unordered_set
  • unordered_multiset
  • unordered_map
  • unordered_multimap

unordered_set

         该容器存储的元素唯一,但没有特定顺序,可根据元素值快速查询;元素的同时也是键,键不可变,因此值也不可改,但可插入和删除;其内部元素不是按特定顺序排序的,而是根据哈希值以快速访问各个元素;unordered_set容器比set容器访问元素更快;

构造函数

其他成员函数

//迭代器
begin(),end()
cbegin(),cend()

size()
empty()
swap ();
clear();
//元素查找
iterator find ( const key_type& k );
size_type count ( const key_type& k ) const;
pair<iterator,iterator> equal_range ( const key_type& k );
//修改
pair <iterator,bool> emplace ( Args&&... args );
iterator emplace_hint ( const_iterator position, Args&&... args );
pair<iterator,bool> insert ( const value_type& val );
iterator erase ( const_iterator position );
size_type bucket_count() const noexcept;
size_type max_bucket_count() const noexcept;
size_type bucket_size ( size_type n ) const;
size_type bucket ( const key_type& k ) const;

float load_factor() const noexcept;
float max_load_factor() const noexcept;
void rehash ( size_type n );
void reserve ( size_type n );

hasher hash_function() const;
key_equal key_eq() const;
allocator_type get_allocator() const noexcept;

unordered_multiset

        该容器存储的元素没有特定顺序,可根据元素值快速查询;与unordered_set非常相似,但可以不同元素拥有相同值;元素的同时也是键,键不可变,因此值也不可改,但可插入和删除;其内部元素不是按特定顺序排序的,而是根据哈希值以快速访问各个元素;相同值的元素在同一bucket中,可迭代遍历其中所有元素;

构造函数

其他成员函数

//迭代器
begin(),end()
cbegin(),cend()

size()
empty()
swap ();
clear();
//元素查找
iterator find ( const key_type& k );
size_type count ( const key_type& k ) const;
pair<iterator,iterator> equal_range ( const key_type& k );
//修改
iterator emplace ( Args&&... args );
iterator emplace_hint ( const_iterator position, Args&&... args );
iterator insert ( const value_type& val );
iterator erase ( const_iterator position );
size_type bucket_count() const noexcept;
size_type max_bucket_count() const noexcept;
size_type bucket_size ( size_type n ) const;
size_type bucket ( const key_type& k ) const;

float load_factor() const noexcept;
float max_load_factor() const noexcept;
void rehash ( size_type n );
void reserve ( size_type n );

hasher hash_function() const;
key_equal key_eq() const;
allocator_type get_allocator() const noexcept;

unordered_map

        该容器是一种关联容器,存储由键值对组成的元素,可根据键值快速查找;通过键值唯一标识元素,对应的映射值类型可能不同;其内部没有特定的排序,而是根据哈希值以快速访问各个元素;可通过键直接访问元素[],比map容器更快;

构造函数

其他成员函数

//迭代器
begin(),end()
cbegin(),cend()

size()
empty()
swap ();
clear();
//访问元素
mapped_type& operator[] ( const key_type& k );
mapped_type& at ( const key_type& k );
//元素查找
iterator find ( const key_type& k );
size_type count ( const key_type& k ) const;
pair<iterator,iterator> equal_range ( const key_type& k );
//修改
pair<iterator, bool> emplace ( Args&&... args );
iterator emplace_hint ( const_iterator position, Args&&... args );
pair<iterator,bool> insert ( const value_type& val );
iterator erase ( const_iterator position );
size_type bucket_count() const noexcept;
size_type max_bucket_count() const noexcept;
size_type bucket_size ( size_type n ) const;
size_type bucket ( const key_type& k ) const;

float load_factor() const noexcept;
float max_load_factor() const noexcept;
void rehash( size_type n );
void reserve ( size_type n );

hasher hash_function() const;
key_equal key_eq() const;
allocator_type get_allocator() const noexcept;

unordered_multimap

        该容器是一种关联容器,存储由键值对组成的元素,与unordered_map非常相似,但允许不同元素拥有相同键值;通过键值唯一标识元素,对应的映射值类型可能不同;其内部没有特定的排序,而是根据哈希值以快速访问各个元素;相同键值的元素在同一bucket中,可迭代遍历其中所有元素;

构造函数

其他成员函数

//迭代器
begin(),end()
cbegin(),cend()

size()
empty()
swap ();
clear();
//元素查找
iterator find ( const key_type& k );
size_type count ( const key_type& k ) const;
pair<iterator,iterator> equal_range ( const key_type& k );
//修改
iterator emplace ( Args&&... args );
iterator emplace_hint ( const_iterator position, Args&&... args );
iterator insert ( const value_type& val );
iterator erase ( const_iterator position );
size_type bucket_count() const noexcept;
size_type max_bucket_count() const noexcept;
size_type bucket_size ( size_type n ) const;
size_type bucket ( const key_type& k ) const;

float load_factor() const noexcept;
float max_load_factor() const noexcept;
void rehash( size_type n );
void reserve( size_type n );

hasher hash_function() const;
key_equal key_eq() const;
allocator_type get_allocator() const noexcept;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值