◐ Shell
clean mode source ↗

标准库标头 <list> - cppreference.com

来自cppreference.com

此头文件是容器库的一部分。

包含

(C++20)

三路比较运算符支持[编辑]
std::initializer_list 类模板[编辑]

双向链表
(类模板) [编辑]

函数

(C++20 移除)(C++20 移除)(C++20 移除)(C++20 移除)(C++20 移除)(C++20)

按照字典顺序比较两个 list 的值
(函数模板) [编辑]
特化 std::swap 算法
(函数模板) [编辑]
擦除所有满足特定判别标准的元素
(函数模板) [编辑]
范围访问

(C++11)(C++14)

返回指向容器或数组起始的迭代器
(函数模板) [编辑]

(C++11)(C++14)

返回指向容器或数组结尾的迭代器
(函数模板) [编辑]
返回指向一个容器或数组的逆向迭代器
(函数模板) [编辑]

(C++14)

返回容器或数组的逆向尾迭代器
(函数模板) [编辑]

(C++17)(C++20)

返回容器或数组的大小
(函数模板) [编辑]

(C++17)

检查容器是否为空
(函数模板) [编辑]

(C++17)

获得指向底层数组的指针
(函数模板) [编辑]

概要

#include <compare>
#include <initializer_list>

namespace std {
  // 类模板 list
  template<class T, class Allocator = allocator<T>> class list;

  template<class T, class Allocator>
  constexpr bool operator==(const list<T, Allocator>& x, const list<T, Allocator>& y);
  template<class T, class Allocator>
  constexpr /*synth-three-way-result*/<T> operator<=>(const list<T, Allocator>& x,
                                                      const list<T, Allocator>& y);

  template<class T, class Allocator>
  constexpr void swap(list<T, Allocator>& x,
                      list<T, Allocator>& y) noexcept(noexcept(x.swap(y)));

  // 擦除
  template<class T, class Allocator, class U = T>
  constexpr typename list<T, Allocator>::size_type erase(list<T, Allocator>& c,
                                                         const U& value);
  template<class T, class Allocator, class Predicate>
  constexpr typename list<T, Allocator>::size_type erase_if(list<T, Allocator>& c,
                                                            Predicate pred);

  namespace pmr {
    template<class T> using list = std::list<T, polymorphic_allocator<T>>;
  }
}

类模板 std::list

namespace std {
  template<class T, class Allocator = allocator<T>> class list
  {
  public:
    // 类型
    using value_type = T;
    using allocator_type = Allocator;
    using pointer = allocator_traits<Allocator>::pointer;
    using const_pointer = allocator_traits<Allocator>::const_pointer;
    using reference = value_type&;
    using const_reference = const value_type&;
    using size_type = /* 由实现定义 */;
    using difference_type = /* 由实现定义 */;
    using iterator = /* 由实现定义 */;
    using const_iterator = /* 由实现定义 */;
    using reverse_iterator = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;

    // 构造/复制/销毁
    constexpr list()
      : list(Allocator())
    {
    }
    constexpr explicit list(const Allocator&);
    constexpr explicit list(size_type n, const Allocator& = Allocator());
    constexpr list(size_type n, const T& value, const Allocator& = Allocator());
    template<class InputIter>
    constexpr list(InputIter first, InputIter last, const Allocator& = Allocator());
    template<container-compatible-range<T> R>
    constexpr list(from_range_t, R&& rg, const Allocator& = Allocator());
    constexpr list(const list& x);
    constexpr list(list&& x);
    constexpr list(const list&, const type_identity_t<Allocator>&);
    constexpr list(list&&, const type_identity_t<Allocator>&);
    constexpr list(initializer_list<T>, const Allocator& = Allocator());
    constexpr ~list();
    constexpr list& operator=(const list& x);
    constexpr list& operator=(list&& x) noexcept(
      allocator_traits<Allocator>::is_always_equal::value);
    constexpr list& operator=(initializer_list<T>);
    template<class InputIter> constexpr void assign(InputIter first, InputIter last);
    template<container-compatible-range<T> R> constexpr void assign_range(R&& rg);
    constexpr void assign(size_type n, const T& t);
    constexpr void assign(initializer_list<T>);
    constexpr allocator_type get_allocator() const noexcept;

    // 迭代器
    constexpr iterator begin() noexcept;
    constexpr const_iterator begin() const noexcept;
    constexpr iterator end() noexcept;
    constexpr const_iterator end() const noexcept;
    constexpr reverse_iterator rbegin() noexcept;
    constexpr const_reverse_iterator rbegin() const noexcept;
    constexpr reverse_iterator rend() noexcept;
    constexpr const_reverse_iterator rend() const noexcept;

    constexpr const_iterator cbegin() const noexcept;
    constexpr const_iterator cend() const noexcept;
    constexpr const_reverse_iterator crbegin() const noexcept;
    constexpr const_reverse_iterator crend() const noexcept;

    // 容量
    constexpr bool empty() const noexcept;
    constexpr size_type size() const noexcept;
    constexpr size_type max_size() const noexcept;
    constexpr void resize(size_type sz);
    constexpr void resize(size_type sz, const T& c);

    // 元素访问
    constexpr reference front();
    constexpr const_reference front() const;
    constexpr reference back();
    constexpr const_reference back() const;

    // 修改器
    template<class... Args> constexpr reference emplace_front(Args&&... args);
    template<class... Args> constexpr reference emplace_back(Args&&... args);
    constexpr void push_front(const T& x);
    constexpr void push_front(T&& x);
    template<container-compatible-range<T> R> constexpr void prepend_range(R&& rg);
    constexpr void pop_front();
    constexpr void push_back(const T& x);
    constexpr void push_back(T&& x);
    template<container-compatible-range<T> R> constexpr void append_range(R&& rg);
    constexpr void pop_back();

    template<class... Args>
    constexpr iterator emplace(const_iterator position, Args&&... args);
    constexpr iterator insert(const_iterator position, const T& x);
    constexpr iterator insert(const_iterator position, T&& x);
    constexpr iterator insert(const_iterator position, size_type n, const T& x);
    template<class InputIter>
    constexpr iterator insert(const_iterator position, InputIter first, InputIter last);
    template<container-compatible-range<T> R>
    constexpr iterator insert_range(const_iterator position, R&& rg);
    constexpr iterator insert(const_iterator position, initializer_list<T> il);

    constexpr iterator erase(const_iterator position);
    constexpr iterator erase(const_iterator position, const_iterator last);
    constexpr void swap(list&) noexcept(
      allocator_traits<Allocator>::is_always_equal::value);
    constexpr void clear() noexcept;

    // list 操作
    constexpr void splice(const_iterator position, list& x);
    constexpr void splice(const_iterator position, list&& x);
    constexpr void splice(const_iterator position, list& x, const_iterator i);
    constexpr void splice(const_iterator position, list&& x, const_iterator i);
    constexpr void splice(const_iterator position,
                          list& x,
                          const_iterator first,
                          const_iterator last);
    constexpr void splice(const_iterator position,
                          list&& x,
                          const_iterator first,
                          const_iterator last);

    constexpr size_type remove(const T& value);
    template<class Predicate> constexpr size_type remove_if(Predicate pred);

    constexpr size_type unique();
    template<class BinaryPredicate>
    constexpr size_type unique(BinaryPredicate binary_pred);

    constexpr void merge(list& x);
    constexpr void merge(list&& x);
    template<class Compare> constexpr void merge(list& x, Compare comp);
    template<class Compare> constexpr void merge(list&& x, Compare comp);

    constexpr void sort();
    template<class Compare> constexpr void sort(Compare comp);

    constexpr void reverse() noexcept;
  };

  template<class InputIter, class Allocator = allocator</*iter-value-type*/<InputIter>>>
  list(InputIter, InputIter, Allocator = Allocator())
    -> list</*iter-value-type*/<InputIter>, Allocator>;

  template<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>>
  list(from_range_t, R&&, Allocator = Allocator())
    -> list<ranges::range_value_t<R>, Allocator>;
}