C++——STL

331 阅读11分钟

STL

术语

  • 概念 concept : 描述一系列的要求
  • 改进 refinement : 表示概念上的继承, 例如, 双向迭代器是正向迭代器的改进
  • 模型 model : 概念的具体实现

容器

容器

STL 有容器概念和容器类型, 概念是具有名称的通用类别, 容器类型是用于创建具体容器对象的模板.

  • 存储对象必须同类型
  • 类型必须是可复制构造或可复制赋值的/可移动构造或可移动复制的

容器接口

X 表示容器类型

成员类型

  • X::allocator_type

  • X::iterator

  • X::value_type

  • X::reference : 等价于 X::value_type&

  • X::const_reference

  • X::pointer

  • X::const_pointer

  • X::iterator

  • X::const_iterator

  • X::reverse_iterator (forward_list 和 无序关联容器没有)

  • X::const_reverse_iterator (forward_list 和 无序关联容器没有)

  • X::difference_type : 用于衡量两个迭代器之间的距离

  • X::size_type : an unsigned integral type

成员函数

  • operator=

    X& operator=(const X&); // 复制赋值运算符
    X& operator=(X&&); // 移动赋值运算符
    X& operator=(initializer_list<X::value_type>); // 初始化器列表
    
  • 迭代器

    X::iterator begin() noexcept;
    X::iterator begin() const noexcept;
    X::const_iterator cbegin() const noexcept;
    
    X::iterator end() noexcept;
    X::const_iterator end() const noexcept;
    X::const_iterator cend() const noexcept;
    // 以上函数在无序关联容器均有接收 size_t 参数的重载版本, 详见无序关联容器
    
    // 以下是 forward_list 和 无序关联容器没有的迭代器
    X::reverse_iterator rbegin() noexcept;
    X::const_reverse_iterator rbegin() const noexcept;
    X::const_reverse_iterator crbegin() const noexcept;
    
    X::reverse_iterator rend() noexcept;
    X::const_reverse_iterator rend() const noexcept;
    X::const_reverse_iterator crend() const noexcept;
    
  • 容量

    X::size_type size() const noexcept; // forward_list 没有该函数
    X::size_type max_size() const noexcept;
    bool empty() const noexcept;
    
  • 修改器

    void swap(X&); // 与另一个容器交换内容
    void clear() noexcept;
    

序列容器

序列(sequence)容器要求元素按照严格的线性顺序排列.

序列容器接口

X 表示容器类型

省略 X:: 限定

  • 构造函数

    // 默认构造函数
    X();
    explicit X (const allocator_type& alloc = allocator_type());
    
    // 填充
    explicit X (size_type n, const allocator_type& alloc = allocator_type());
    
    X (size_type n, const value_type& val, 
       const allocator_type& alloc = allocator_type());
    
    // 区间
    template<typename InputIterator>
    X (InputIterator first, InputIterator last, 
       const allocator_type& alloc = allocator_type());
    
    // 复制
    X (const X&);
    X (const X&, const allocator_type&);
    
    // 移动
    X (X&&);
    X (X&&, const allocator_type&);
    
    // 初始化器列表
    X (initializer_list<value_type> il, 
      const allocator_type& alloc = allocator_type());
    
  • 重载比较运算符 : == != <= >= < >

    例如 :

    template<typename T, typename Alloc>
    bool operator==(const X<T, Alloc>& l, const X<T, Alloc>& r);
    
  • 容量

    void resize(size_type n);
    void resize(size_type n, const value_type& val); // 如果 n 大于当前容量则用 val 填充
    
  • 修改器

    // assign (分配新内容, 线性时间)
    template <typename InputIterator>
    void assign(InputIterator first, InputIterator last); // 范围型
    
    void assign(size_type n, const value_type& val); // 填充型
    
    void assign(initializer_list<value_type> il); // 初始化列表
    
    // insert 线性时间 (forward_list 没有)
    iterator insert(const_iterator position, const value_type& val); // 单元素插入
    
    iterator insert(const_iterator position, size_type n, const value_type& val); // 批量填充插入
    
    template<typename InputIterator>
        iterator insert(const_iterator position, InputIterator first, InputIterator last); // 范围型
    
    iterator insert(const_iterator position, value_type&& val); // 移动
    
    iterator insert(const_iterator position, initializer_list<value_type> il); // 初始化列表
    
    // erase 线性时间 (forward_list 没有)
    iterator erase(const_iterator position);
    iterator erase(const_iterator first, const_iterator last);
    
    // emplace 线性时间 (forward_list 没有)
    template<typename... Args>
        iterator emplace(const_iterator position, Args&&... args); // 通过插入新元素到指定 position 来扩展容器. 新的元素通过参数构造, 与 insert 不同, 这里是通过参数构造元素, 而不是拷贝元素
    

emplace 用例

class C{
public:
    int i;
    int j;
    int k;
    C(int _i, int _j, int _k) : i(_i), j(_j), k(_k) {}
};

int main() {
    vector<C> c;
    c.emplace(c.begin(), 1, 2, 3);
    return 0;
}
vector
template <typename T, typename Alloc = allocator<T>> class vector;

容器属性

  • 序列容器
  • 动态数组

成员函数

  • 容量

    size_type capacity();
    void reserve(size_type n); // 让 capacity 刚好装下 n 个 元素
    void shrink_to_fit();
    
  • 元素访问 (全部 O(1)O(1) 复杂度)

    reference operator[] (size_type n);
    const_reference operator[] (size_type n) const;
    
    reference at(size_type n);
    const_reference at(size_type n) const;
    // 以上两种区别为 : [] 越界行为未定义, at 会抛出异常
    
    reference front();
    const_reference front() const;
    
    reference back();
    const_reference back() const;
    
    value_type* data();
    const value_type* data() const;
    
  • 修改器

    // push_back O(1)
    void push_back(const value_type& val);
    void push_back(value_type&& val);
    
    // pop_back O(1)
    void pop_back();
    
    
    // emplace_back 常量时间
    template<typename... Args>
    	void emplace_back(Args&&... args);
    
deque
tempalte <typename T, typename Alloc= allocator<T>> class deque;	

容器属性

  • 序列容器
  • 动态数组

成员函数

  • 容量

    void shrink_to_fit();
    
  • 元素访问

    reference operator[] (size_type n);
    const_reference operator[] (size_type n) const;
    
    reference at(size_type n);
    const_reference at(size_type n) const;
    // 以上两种区别为 : [] 越界行为未定义, at 会抛出异常
    
    reference front();
    const_reference front() const;
    
    reference back();
    const_reference back() const;
    
  • 修改器

    // push_back O(1)
    void push_back(const value_type& val);
    void push_back(value_type&& val);
    
    // pop_back O(1)
    void pop_back();
    
    // push_front O(1)
    void push_front (const value_type& val);
    void push_front (value_type&& val);
    
    // pop_front O(1)
    void pop_front();
    
    
    // emplace_back O(1)
    template<typename... Args>
        void emplace_back(Args&&... args);
    
    // emplace_front O(1)
    template <typename... Args>
      void emplace_front (Args&&... args);
    
list
template <typename T, typename Alloc = allocator<T>> class list;

容器属性

  • 序列容器
  • 双向链表

成员函数

  • 元素访问

    reference front();
    const_reference front() const;
    
    reference back();
    const_reference back() const;
    
  • 修改器

    // push_back O(1)
    void push_back(const value_type& val);
    void push_back(value_type&& val);
    
    // pop_back O(1)
    void pop_back();
    
    // push_front O(1)
    void push_front (const value_type& val);
    void push_front (value_type&& val);
    
    // pop_front O(1)
    void pop_front();
    
    
    // emplace_back O(1)
    template<typename... Args>
        void emplace_back(Args&&... args);
    
    // emplace_front O(1)
    template <typename... Args>
      void emplace_front (Args&&... args);
    
  • 操作

    // 将整个 x 列表的元素 splice 到 position 上
    void splice (const_iterator position, list& x);
    void splice (const_iterator position, list&& x);
    
    // 将 x 中 i 指向的单个元素 splice 到 position 上
    void splice (const_iterator position, list& x, const_iterator i);
    void splice (const_iterator position, list&& x, const_iterator i);
    
    // 将 x 在 [first, last) 区间上的元素 splice 到 position 上
    void splice (const_iterator position, list& x,
                 const_iterator first, const_iterator last);
    void splice (const_iterator position, list&& x,
                 const_iterator first, const_iterator last);
    
    void remove (const value_type& val);
    
    template <typename Predicate>
      void remove_if (Predicate pred);
    
    void unique();
    
    template <typename BinaryPredicate>
      void unique (BinaryPredicate binary_pred);
    
    void merge (list& x);
    void merge (list&& x);
    
    template <typename Compare>
      void merge (list& x, Compare comp);
    template <typename Compare>
      void merge (list&& x, Compare comp);
    
    void sort();
    
    template <typename Compare>
      void sort (Compare comp);
    
    void reverse() noexcept;
    
forward_list (C++ 11)
template<typename T, typename Alloc = allocator<T>> class forward_list;

容器属性

  • 序列容器
  • 链表

可能是用头插法的链表 ?

成员函数

  • 迭代器

    iterator before_begin() noexcept;
    const_iterator before_begin() const noexcept;
    const_iterator cbefore_begin() const noexcept;
    
  • 元素访问

    reference front();
    const_reference front() const;
    
  • 修改器

    void push_front (const value_type& val);
    void push_front (value_type&& val);
    
    void pop_front();
    
    // insert_after
    iterator insert_after ( const_iterator position, const value_type& val ); // 定点左值插入
    
    iterator insert_after ( const_iterator position, value_type&& val ); // 定点右值插入
    
    iterator insert_after ( const_iterator position, size_type n, const value_type& val ); // 定点批量左值插入
    
    template <typename InputIterator>
      iterator insert_after ( const_iterator position, InputIterator first, InputIterator last ); // range
    
    iterator insert_after ( const_iterator position, initializer_list<value_type> il ); // 初始化列表
    
    // emplace_front
    template <typename... Args>
      void emplace_front (Args&&... args);
    
    // emplace_after
    template <typename... Args>
      iterator emplace_after (const_iterator position, Args&&... args);
    
    // erase_after
    iterator erase_after (const_iterator position);
    iterator erase_after (const_iterator position, const_iterator last);
    
  • 操作

    // entire list (1)	整个列表
    void splice_after (const_iterator position, forward_list& fwdlst);
    
    void splice_after (const_iterator position, forward_list&& fwdlst);
    
    // single element (2)	指定值
    void splice_after (const_iterator position, forward_list& fwdlst, const_iterator i);
    
    void splice_after (const_iterator position, forward_list&& fwdlst, const_iterator i);
    
    // element range (3) 指定范围
    void splice_after (const_iterator position, forward_list& fwdlst, const_iterator first, const_iterator last);
    
    void splice_after (const_iterator position, forward_list&& fwdlst, const_iterator first, const_iterator last);
    
    void remove (const value_type& val);
    
    template <typename Predicate>
      void remove_if (Predicate pred);
    
    void unique();
    
    template <typename BinaryPredicate>
      void unique (BinaryPredicate binary_pred);
    
    void merge (list& x);
    void merge (list&& x);
    
    template <typename Compare>
      void merge (list& x, Compare comp);
    template <typename Compare>
      void merge (list&& x, Compare comp);
    
    void sort();
    
    template <typename Compare>
      void sort (Compare comp);
    
    void reverse() noexcept;
    

关联容器

关联容器将键与值联系, 并使用键查值.

优点在于提供了对元素的快速访问. 与序列容器相似, 允许插入新元素, 但不能指定插入位置.

通常通过某种树实现(红黑树).

关联容器接口

成员类型

  • key_type
  • key_compare
  • value_compare
  • mapped_type (mapmultimap 特有)

成员函数

  • 构造器

    // empty (1)	
    X();
    
    explicit X(const key_compare& comp, const allocator_type& alloc = allocator_type());
    
    explicit X(const allocator_type& alloc);
    
    // range (2)	
    template <typename InputIterator>
      X(InputIterator first, InputIterator last,
        const key_compare& comp = key_compare(),
        const allocator_type& = allocator_type());
    
    template <typename InputIterator>
      X(InputIterator first, InputIterator last,
        const allocator_type& = allocator_type());
    
    // copy (3)	
    X(const X& x);
    X(const X& x, const allocator_type& alloc);
    
    // move (4)	
    X(X&& x);
    X(X&& x, const allocator_type& alloc);
    
    // initializer list (5)	
    X(initializer_list<value_type> il,
      const key_compare& comp = key_compare(),
      const allocator_type& alloc = allocator_type());
    
    X(initializer_list<value_type> il,
      const allocator_type& alloc = allocator_type());
    
  • 修改

    template <typename... Args>
    iterator emplace_hint ( const_iterator position, Args&&... args );
    
  • Observers

    key_compare key_comp() const;
    value_compare value_comp() const;
    
set & multiset

共同接口

  • 修改 (拥有三个共同的 inserterase 的所有重载版本)

    // with hint
    iterator insert(const_iterator position, const value_type& val);
    
    iterator insert(const_iterator position, value_type&& val);
    
    // range
    template <typename InputIterator>
      void insert(InputIterator first, InputIterator last);
    
    // initializer list
    void insert(initializer_list<value_type> il);
    
    iterator erase(const_iterator position);
    
    size_type erase(const value_type& val);
    
    iterator erase(const_iterator first, const_iterator last);
    
  • 操作

    const_iterator find(const value_type& val) const;
    iterator find(const value_type& val);
    
    size_type count(const value_type& val) const;
    
    iterator lower_bound(const value_type& val);
    const_iterator lower_bound(const value_type& val) const;
    
    iterator upper_bound(const value_type& val);
    const_iterator upper_bound(const value_type& val) const;
    
    pair<const_iterator,const_iterator> equal_range(const value_type& val) const;
    
    pair<iterator,iterator> equal_range(const value_type& val);
    

set

template <typename T,
			typename Compare = less<T>,
			typename Alloc = allocator<T>>
class set;

set 是以一定顺序存储唯一元素的容器.

set 中, 元素自己为键和值, 并且每个值都是唯一的.

在内部, 元素总是排序的.

容器属性

  • 关联的
  • 有序的

成员函数

  • 修改

    // single element insert
    pair<iterator,bool> insert(const value_type& val);
    pair<iterator,bool> insert(value_type&& val);
    
    template <typename... Args>
      pair<iterator,bool> emplace(Args&&... args);
    

multiset

template <typename T,
           typename Compare = less<T>,
           typename Alloc = allocator<T>>
class multiset;

以一定顺序存储元素, 可以有重复值.

容器属性

  • 关联的
  • 有序的
  • 允许重复元素

成员函数

  • 修改

    // single element (1)	
    iterator insert(const value_type& val);
    iterator insert(value_type&& val);
    
    template <typename... Args>
      iterator emplace(Args&&... args)
    
map & multimap

成员类型

  • mapped_type

共同接口

  • 修改 (拥有三个相同的 inserterase 的所有重载版本)

    // with hint	
    iterator insert(const_iterator position, const value_type& val);
    template <typename P> iterator insert(const_iterator position, P&& val);
    
    // range
    template <typename InputIterator>
      void insert(InputIterator first, InputIterator last);
    
    // initializer list
    void insert(initializer_list<value_type> il);
    
    iterator erase(const_iterator position);
    
    size_type erase(const key_type& k);
    
    iterator erase(const_iterator first, const_iterator last);
    
  • 操作 (这些方法于 setmultiset 不同点在于参数为 key_typesetmultiset 的参数为 value_type, 其实也可以看作一样, 因为 set 中 key_typevalue_type 是一样的)

    iterator find(const key_type& k);
    const_iterator find(const key_type& k) const;
    
    size_type count(const key_type& k) const;
    
    iterator lower_bound(const key_type& k);
    const_iterator lower_bound(const key_type& k) const;
    
    iterator upper_bound(const key_type& k);
    const_iterator upper_bound(const key_type& k) const;
    
    pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
    
    pair<iterator,iterator> equal_range(const key_type& k);
    

map

template < typename Key,
           typename T,
           typename Compare = less<Key>,
           typename Alloc = allocator<pair<const Key,T>>>
class map;

map 是以 key valuemapped value 组合对为存储类型的容器, 并以一定顺序存储.

mapkey values 用于排序并唯一标识元素, 而 mapped values 存储与这个 key 关联的内容.

key valuemapped value 的类型可能不同, 并且组合成成员类型 value_type, 它是一个如下的 typedef 类型 :

typedef pair<const Key, T> value_type;

容器属性

  • 关联的
  • 排序的
  • 键唯一

成员函数

  • 元素访问

    mapped_type& operator[](const key_type& k);
    mapped_type& operator[](key_type&& k);
    
    mapped_type& at(const key_type& k);
    const mapped_type& at(const key_type& k) const;
    
  • 修改

    // single element (1)	
    pair<iterator,bool> insert(const value_type& val);
    template <typename P> pair<iterator,bool> insert(P&& val);
    
    template <typename... Args>
      pair<iterator,bool> emplace(Args&&... args);
    

multimap

template < typename Key,
           typename T,
           typename Compare = less<Key>,
           typename Alloc = allocator<pair<const Key,T>>>
class multimap;

多个元素可以有相同的 key value

容器属性

  • 关联的
  • 有序的
  • 允许重复键值

成员函数

  • 修改

    // single element insert
    iterator insert(const value_type& val);
    template <typename P> iterator insert(P&& val);
    
    template <typename... Args>
      iterator emplace(Args&&... args);
    

无序关联容器

无序关联容器是基于 hash 的实现, 用于 O(1) 快速查找.

无序关联容器接口

成员类型

  • key_type
  • key_equal
  • mapped_type (unorder_map unorder_multimap 特有)
  • hasher
  • local_iterator : 用于 hash 表的桶迭代器
  • const_local_iterator

成员函数

  • 构造器

    // empty (1)	
    X();
    
    explicit X(size_type n,
               const hasher& hf = hasher(),
               const key_equal& eql = key_equal(),
               const allocator_type& alloc = allocator_type());
    
    explicit X(const allocator_type& alloc);
    
    X(size_type n, const allocator_type& alloc);
    
    X(size_type n, const hasher& hf, const allocator_type& alloc);
    
    // range (2)	
    template <typename InputIterator>
      X(InputIterator first, InputIterator last,
        size_type n = /* see below */,
        const hasher& hf = hasher(),
        const key_equal& eql = key_equal(),
        const allocator_type& alloc = allocator_type() );
    
    template <typename InputIterator>
      X(InputIterator first, InputIterator last,     	 size_type n, const allocator_type& alloc);
    
    template <typename InputIterator>
      X(InputIterator first, InputIterator last, 	    	size_type n, const hasher& hf, const allocator_type& alloc);
    
    // copy (3)	
    X(const X& x);
    X(const X& x, const allocator_type& alloc);
    
    // move (4)	
    X(X&& x);
    X(X&& x, const allocator_type& alloc );
    
    // initializer list (5)	
    X(initializer_list<value_type> il,
      size_type n = /* see below */,
      const hasher& hf = hasher(),
      const key_equal& eql = key_equal(),
      const allocator_type& alloc = allocator_type());
    
    X(initializer_list<value_type> il, size_type n,
      const allocator_type& alloc);
    
    X(initializer_list<value_type> il, size_type n,
      const hasher& hf, const allocator_type& alloc );
    

    size_type n 是 hash 初始桶的最小数. 并不是指容器的容量. 如果没有指定, 构造器会自动决定决定该值 (不同库的实现也不同)

  • 迭代器 (提供了接收每个桶编号为参数的迭代器)

    local_iterator begin(size_type n);
    const_local_iterator begin(size_type n) const;
    
    local_iterator end(size_type n);
    const_local_iterator end(size_type n) const;
    
    const_local_iterator cbegin(size_type n) const;
    
    const_local_iterator cend(size_type n) const;
    

    例如 :

    int main () {
        unordered_multiset<string> set{"hello", "hello", "world", "word"};
        
        for (auto i = 0; i < set.bucket_count(); i++) {
            cout << "bucket #" << i << " contains:";
            for (auto local_it = set.begin(i); it != set.end(i); local_it++) {
                cout << " " << *local_it;
            }
            cout << endl;
        }
        return 0;
    }
    
    // 输出 : 
    // bucket #0 contains: world hello hello
    // bucket #1 contains:
    // bucket #2 contains: word
    // bucket #3 contains:
    // bucket #4 contains:
    
  • 元素查找

    iterator find(const key_type& k);
    const_iterator find(const key_type& k) const;
    
    size_type count(const key_type& k) const;
    
    pair<iterator,iterator>
      equal_range(const key_type& k);
    pair<const_iterator,const_iterator>
      equal_range(const key_type& k) const;
    
  • 修改

    template <typename... Args>
    iterator emplace_hint(const_iterator position, Args&&... args);
    
    // by position (1)	
    iterator erase(const_iterator position);
    // by key (2)	
    size_type erase(const key_type& k);
    // range (3)	
    iterator erase(const_iterator first, const_iterator last);
    
  • == != <= >= < >

  • Observers

    hasher hash_function() const;
    key_equal key_eq() const;
    
  • hash 表, 桶相关

    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; // 返回指定 key 所在的桶号
    
unordered_set & unordered_multiset

共有接口

  • 修改

    pair<iterator,bool> insert(value_type&& val );
    
    iterator insert(const_iterator hint, const value_type& val );
    
    iterator insert(const_iterator hint, value_type&& val );
    
    template <typename InputIterator>
        void insert(InputIterator first, InputIterator last );
    
    void insert(initializer_list<value_type> il );
    

unordered_set

tempalte <typename Key, 
			typename Hash = hash<key>,
			typename Predi = equal_to<Key>,
			typename Alloc = allocator<Key>>
class unorder_set;

容器属性

  • 关联的
  • 无序的
  • 键唯一

成员函数

  • 修改

    pair<iterator,bool> insert(const value_type& val);
    pair<iterator,bool> insert(value_type&& val);
    
    template <typename... Args>
    pair <iterator,bool> emplace(Args&&... args);
    

unordered_multiset

template <typename Key,
		  typename Hash = hash<Key>,
		  typename Predi = equal_to<Key>,
		  typename Alloc = allocator<Key>>
class unordered_multiset;

容器属性

  • 关联的
  • 无序的
  • 允许重复键

成员函数

  • 修改

    iterator insert(const value_type& val);
    iterator insert(value_type&& val);
    
    template <typename... Args> iterator emplace(Args&&... args);
    
unordered_map & unordered_multimap

unorder_map

template <typename Key,
		  typename T,
		  typename Hash = hash<Key>,
		  typename Predi = equal_to<Key>,
		  typename Alloc = allocator<pair<const Key, T>>>
class unordered_map;

容器属性

  • 关联的
  • 无序的
  • 键唯一

成员函数

  • 元素访问

    mapped_type& operator[](const key_type& k);
    mapped_type& operator[](key_type&& k);
    
    mapped_type& at(const key_type& k);
    const mapped_type& at(const key_type& k) const;
    
  • 修改

    pair<iterator,bool> insert(const value_type& val);
    
    template <typename P>
        pair<iterator,bool> insert(P&& val);
    
    template <typename... Args>
    pair<iterator, bool> emplace(Args&&... args);
    

unordered_multimap

template <typename Key,
		  typename T,
		  typename Hash = hash<Key>,
		  typename Predi = equal_to<Key>,
		  typename Alloc = allocator<pair<const Key, T>>>
class unordered_multimap;

容器属性

  • 关联的
  • 无序的
  • 允许重复键

成员函数

  • 修改

    iterator insert(const value_type& val);
    
    template <typename P>
        iterator insert(P&& val);
    
    template <typename... Args> iterator emplace(Args&&... args);
    

容器适配器

容器适配器是序列/关联容器的变体. 它们为了简化和清楚化而限制了容器接口. ==容器适配器不支持迭代器==

序列容器适配器

序列容器适配器成员类型

member typedefinitionnotes
value_typeThe first template parameter (T)Type of the elements
container_typeThe second template parameter (Container)Type of the underlying container
referencecontainer_type::referenceusually, value_type&
const_referencecontainer_type::const_referenceusually, const value_type&
size_typean unsigned integral typeusually, the same as size_t
queue
template<typename T, typename Container = deque<T>> class deque;
  • 成员类型

    value_type

    container_type

    reference

    const_reference

    size_type

  • 构造器

    explicit queue (const container_type& ctnr);
    
    explicit queue (container_type&& ctnr = container_type());
    
    template <typename Alloc> explicit queue (const Alloc& alloc);
    
    template <typename Alloc> queue (const container_type& ctnr, const Alloc& alloc);
    
    template <typename Alloc> queue (container_type&& ctnr, const Alloc& alloc);
    
    template <typename Alloc> queue (const queue& x, const Alloc& alloc);
    
    template <typename Alloc> queue (queue&& x, const Alloc& alloc);
    
  • empty

  • size

  • front

  • back

  • push

  • emplace

  • pop

  • swap

  • == != <= >= < >

priority_queue
template <typename T, typename Container = vector<T>,
  typename Compare = less<typename Container::value_type> > class priority_queue;
  • 成员类型

    value_type

    container_type

    reference

    const_reference

    size_type

  • 构造器

    priority_queue (const Compare& comp, const Container& ctnr);
    
    // range
    template <typename InputIterator>
      priority_queue (InputIterator first, InputIterator last, const Compare& comp, const Container& ctnr);
    
    // move
    explicit priority_queue (const Compare& comp = Compare(), Container&& ctnr = Container());
    
    // move-range
    template <typename InputIterator>
      priority_queue (InputIterator first, InputIterator last, const Compare& comp, Container&& ctnr = Container());
    
    // allocator version
    template <typename Alloc> explicit priority_queue (const Alloc& alloc);
    
    template <typename Alloc> priority_queue (const Compare& comp, const Alloc& alloc);
    
    template <typename Alloc> priority_queue (const Compare& comp, const Container& ctnr, const Alloc& alloc);
    
    template <typename Alloc> priority_queue (const Compare& comp, Container&& ctnr, const Alloc& alloc);
    
    template <typename Alloc> priority_queue (const priority_queue& x, const Alloc& alloc);
    
    template <typename Alloc> priority_queue (priority_queue&& x, const Alloc& alloc);
    
  • empty

  • size

  • top

  • push

  • emplace

  • pop

  • swap

stack
template <typename T, typename Container = deque<T> > class stack;

成员类型

  • value_type

  • container_type

  • reference

  • const_reference

  • size_type

成员函数

  • 构造器

    explicit stack (const container_type& ctnr);
    
    explicit stack (container_type&& ctnr = container_type());
    
    template <typename Alloc> explicit stack (const Alloc& alloc);
    
    template <typename Alloc> stack (const container_type& ctnr, const Alloc& alloc);
    
    template <typename Alloc> stack (container_type&& ctnr, const Alloc& alloc);
    
    template <typename Alloc> stack (const stack& x, const Alloc& alloc);
    
    template <typename Alloc> stack (stack&& x, const Alloc& alloc);
    
  • size

  • top

  • push

  • emplace

  • pop

  • swap

  • 所有比较运算符

迭代器

不同算法对迭代器的需求不同, 基本分为以下几类 :

  • InputIterator : 单向, 只读, 支持 ++ 和解引用操作, 不支持解引用写入

  • OutputIterator : 单项, 只写, 支持 ++, 不支持解引用, 支持解引用写入

  • ForwardIterator : 总是按照相同顺序遍历一系列值

  • BidirectionalIterator : 双向迭代器, 支持 ++-- 操作

  • RandomIterator : 支持随机访问

    随机迭代器 ri 支持操作

    • ri + n

    • n + ri

    • ri - n

    • ri += n

    • ri -= n

    • ri[n]

    • ri1 - ri0

    • ri0 < ri1

    • ri0 > ri1

    • ri0 >= ri1

    • ri0 <= ri1

函数对象

许多 STL 算法使用函数对象 —— 也叫函数符号 (functor). 函数符是可以以函数方式与 () 结合使用的任意对象(例如函数本身, 重载了 () 符的对象...)

  • 生成器 (generator) : 不用参数就可以调用的函数符
  • 一元函数 (unary function)
  • 二元函数 (binary function)
  • 谓词(predicate) : 返回 bool 的一元函数
  • 二元谓词 (binary predicate) : 返回 bool 的二元函数

头文件 functional 定义了许多预定义的函数符 :

运算符相应函数符
+plus
-minus
*multiplies
/divides
%modulus
-negate
==equal_to
!=note_equal_to
>greater
<less
>=greater_equal
<=less_equal
&&logic_and
``logic_or
!logic_not

算法

template <typename BidirectionalIterator>
  void reverse (BidirectionalIterator first, BidirectionalIterator last);

// default (1)
template <typename RandomAccessIterator>
  void sort (RandomAccessIterator first, RandomAccessIterator last);

// custom (2)
template <typename RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

// default (1)	
template <typename ForwardIterator, typename T>
  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val);

// custom (2)	
template <typename ForwardIterator, typename T, typename Compare>
  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val, Compare comp);