Namespaces
Variants

Standard library header <flat_map> (C++23)

From cppreference.net
Standard library headers

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

目录

包含头文件

(C++20)
三路比较运算符 支持
std::initializer_list 类模板

(C++23)
适配两个容器以提供按键排序的唯一键值对集合
(类模板)
适配两个容器以提供按键排序的键值对集合
(类模板)
特化 std::uses_allocator 类型特征
(类模板特化)
特化 std::uses_allocator 类型特征
(类模板特化)

函数

擦除满足特定条件的所有元素
(函数模板)
擦除满足特定条件的所有元素
(函数模板)

标签

指示范围中的元素已排序且唯一
(标签)
指示范围中的元素已排序(不要求唯一性)
(标签)

概要

#include <compare>
#include <initializer_list>
namespace std {
  // 类模板 flat_map
  template<class Key,
           class T,
           class Compare         = less<Key>,
           class KeyContainer    = vector<Key>,
           class MappedContainer = vector<T>>
  class flat_map;
  struct sorted_unique_t
  {
    explicit sorted_unique_t() = default;
  };
  inline constexpr sorted_unique_t sorted_unique{};
  template<class Key,
           class T,
           class Compare,
           class KeyContainer,
           class MappedContainer,
           class Allocator>
  struct uses_allocator<flat_map<Key, T, Compare, KeyContainer, MappedContainer>,
                        Allocator>;
  // flat_map 的擦除操作
  template<class Key,
           class T,
           class Compare,
           class KeyContainer,
           class MappedContainer,
           class Predicate>
  typename flat_map<Key, T, Compare, KeyContainer, MappedContainer>::size_type erase_if(
    flat_map<Key, T, Compare, KeyContainer, MappedContainer>& c,
    Predicate pred);
  // 类模板 flat_multimap
  template<class Key,
           class T,
           class Compare         = less<Key>,
           class KeyContainer    = vector<Key>,
           class MappedContainer = vector<T>>
  class flat_multimap;
  struct sorted_equivalent_t
  {
    explicit sorted_equivalent_t() = default;
  };
  inline constexpr sorted_equivalent_t sorted_equivalent{};
  template<class Key,
           class T,
           class Compare,
           class KeyContainer,
           class MappedContainer,
           class Allocator>
  struct uses_allocator<flat_multimap<Key, T, Compare, KeyContainer, MappedContainer>,
                        Allocator>;
  // flat_multimap 的擦除操作
  template<class Key,
           class T,
           class Compare,
           class KeyContainer,
           class MappedContainer,
           class Predicate>
  typename flat_multimap<Key, T, Compare, KeyContainer, MappedContainer>::size_type
  erase_if(flat_multimap<Key, T, Compare, KeyContainer, MappedContainer>& c,
           Predicate pred);
}

类模板 std:: flat_map

namespace std {
  template<class Key,
           class T,
           class Compare         = less<Key>,
           class KeyContainer    = vector<Key>,
           class MappedContainer = vector<T>>
  class flat_map
  {
  public:
    // 类型定义
    using key_type               = Key;
    using mapped_type            = T;
    using value_type             = pair<key_type, mapped_type>;
    using key_compare            = Compare;
    using reference              = pair<const key_type&, mapped_type&>;
    using const_reference        = pair<const key_type&, const mapped_type&>;
    using size_type              = size_t;
    using difference_type        = ptrdiff_t;
    using iterator               = /* 实现定义 */;
    using const_iterator         = /* 实现定义 */;
    using reverse_iterator       = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;
    using key_container_type     = KeyContainer;
    using mapped_container_type  = MappedContainer;
    class value_compare
    {
    private:
      key_compare comp; // 仅用于说明
      value_compare(key_compare c)
        : comp(c)
      {
      } // 仅用于说明
    public:
      bool operator()(const_reference x, const_reference y) const
      {
        return comp(x.first, y.first);
      }
    };
    struct containers
    {
      key_container_type keys;
      mapped_container_type values;
    };
    // 构造函数
    flat_map()
      : flat_map(key_compare())
    {
    }
    explicit flat_map(const key_compare& comp)
      : c()
      , compare(comp)
    {
    }
    flat_map(key_container_type key_cont,
             mapped_container_type mapped_cont,
             const key_compare& comp = key_compare());
    flat_map(sorted_unique_t,
             key_container_type key_cont,
             mapped_container_type mapped_cont,
             const key_compare& comp = key_compare());
    template<class InputIter>
    flat_map(InputIter first, InputIter last, const key_compare& comp = key_compare())
      : c()
      , compare(comp)
    {
      insert(first, last);
    }
    template<class InputIter>
    flat_map(sorted_unique_t s,
             InputIter first,
             InputIter last,
             const key_compare& comp = key_compare())
      : c()
      , compare(comp)
    {
      insert(s, first, last);
    }
    template<container-compatible-range<value_type> R>
    flat_map(from_range_t fr, R&& rg)
      : flat_map(fr, std::forward<R>(rg), key_compare())
    {
    }
    template<container-compatible-range<value_type> R>
    flat_map(from_range_t, R&& rg, const key_compare& comp)
      : flat_map(comp)
    {
      insert_range(std::forward<R>(rg));
    }
    flat_map(initializer_list<value_type> il, const key_compare& comp = key_compare())
      : flat_map(il.begin(), il.end(), comp)
    {
    }
    flat_map(sorted_unique_t s,
             initializer_list<value_type> il,
             const key_compare& comp = key_compare())
      : flat_map(s, il.begin(), il.end(), comp)
    {
    }
    // 带分配器的构造函数
    template<class Alloc>
    explicit flat_map(const Alloc& a);
    template<class Alloc>
    flat_map(const key_compare& comp, const Alloc& a);
    template<class Alloc>
    flat_map(const key_container_type& key_cont,
             const mapped_container_type& mapped_cont,
             const Alloc& a);
    template<class Alloc>
    flat_map(const key_container_type& key_cont,
             const mapped_container_type& mapped_cont,
             const key_compare& comp,

类模板 std:: flat_multimap

namespace std {
  template<class Key,
           class T,
           class Compare         = less<Key>,
           class KeyContainer    = vector<Key>,
           class MappedContainer = vector<T>>
  class flat_multimap
  {
  public:
    // 类型定义
    using key_type               = Key;
    using mapped_type            = T;
    using value_type             = pair<key_type, mapped_type>;
    using key_compare            = Compare;
    using reference              = pair<const key_type&, mapped_type&>;
    using const_reference        = pair<const key_type&, const mapped_type&>;
    using size_type              = size_t;
    using difference_type        = ptrdiff_t;
    using iterator               = /* 实现定义 */;
    using const_iterator         = /* 实现定义 */;
    using reverse_iterator       = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;
    using key_container_type     = KeyContainer;
    using mapped_container_type  = MappedContainer;
    class value_compare
    {
    private:
      key_compare comp; // 仅用于说明
      value_compare(key_compare c)
        : comp(c)
      {
      } // 仅用于说明
    public:
      bool operator()(const_reference x, const_reference y) const
      {
        return comp(x.first, y.first);
      }
    };
    struct containers
    {
      key_container_type keys;
      mapped_container_type values;
    };
    // 构造函数
    flat_multimap()
      : flat_multimap(key_compare())
    {
    }
    explicit flat_multimap(const key_compare& comp)
      : c()
      , compare(comp)
    {
    }
    flat_multimap(key_container_type key_cont,
                  mapped_container_type mapped_cont,
                  const key_compare& comp = key_compare());
    flat_multimap(sorted_equivalent_t,
                  key_container_type key_cont,
                  mapped_container_type mapped_cont,
                  const key_compare& comp = key_compare());
    template<class InputIter>
    flat_multimap(InputIter first,
                  InputIter last,
                  const key_compare& comp = key_compare())
      : c()
      , compare(comp)
    {
      insert(first, last);
    }
    template<class InputIter>
    flat_multimap(sorted_equivalent_t s,
                  InputIter first,
                  InputIter last,
                  const key_compare& comp = key_compare())
      : c()
      , compare(comp)
    {
      insert(s, first, last);
    }
    template<container-compatible-range<value_type> R>
    flat_multimap(from_range_t fr, R&& rg)
      : flat_multimap(fr, std::forward<R>(rg), key_compare())
    {
    }
    template<container-compatible-range<value_type> R>
    flat_multimap(from_range_t, R&& rg, const key_compare& comp)
      : flat_multimap(comp)
    {
      insert_range(std::forward<R>(rg));
    }
    flat_multimap(initializer_list<value_type> il,
                  const key_compare& comp = key_compare())
      : flat_multimap(il.begin(), il.end(), comp)
    {
    }
    flat_multimap(sorted_equivalent_t s,
                  initializer_list<value_type> il,
                  const key_compare& comp = key_compare())
      : flat_multimap(s, il.begin(), il.end(), comp)
    {
    }
    // 带分配器的构造函数
    template<class Alloc>
    explicit flat_multimap(const Alloc& a);
    template<class Alloc>
    flat_multimap(const key_compare& comp, const Alloc& a);
    template<class Alloc>
    flat_multimap(const key_container_type& key_cont,
                  const mapped_container_type& mapped_cont,
                  const Alloc& a);
    template<class Alloc>
    flat_multimap(const key_container_type& key_cont,
                  const</

参考文献

  • C++23 标准 (ISO/IEC 14882:2024):
  • 24.6.4 头文件 <flat_map> 概要 [flat.map.syn]
  • 24.6.9.2 定义 [flat.map.defn]
  • 24.6.10.2 定义 [flat.multimap.defn]