Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::flat_set

      From cppreference.com
      <cpp‎ |container
       
       
       
       
      Defined in header<flat_set>
      template<

         class Key,
         class Compare=std::less<Key>,
         class KeyContainer=std::vector<Key>

      >class flat_set;
      (since C++23)

      The flat set is acontainer adaptor that gives the functionality of an associative container that stores a sorted set of unique objects of typeKey. Sorting is done using the key comparison functionCompare.

      The class templateflat_set acts as a wrapper to the underlying sorted container passed as object of typeKeyContainer.

      Everywhere the standard library uses theCompare requirements, uniqueness is determined by using the equivalence relation. Informally, two objectsa andb are considered equivalent if neither compares less than the other:!comp(a, b)&&!comp(b, a).

      std::flat_set meets the requirements ofContainer,ReversibleContainer,optional container requirements, and all requirements ofAssociativeContainer (including logarithmic search complexity), except that:

      • requirements related to nodes are not applicable,
      • iterator invalidation requirements differ,
      • the complexity of insertion and erasure operations is linear.

      A flat set supports mostAssociativeContainer's operations that use unique keys.

      All member functions ofstd::flat_set areconstexpr: it is possible to create and usestd::flat_set objects in the evaluation of a constant expression.

      However,std::flat_set objects generally cannot beconstexpr, because any dynamically allocated storage must be released in the same evaluation of constant expression.

      (since C++26)

      Contents

      [edit]Iterator invalidation

      This section is incomplete

      [edit]Template parameters

      Key - The type of the stored elements. The program is ill-formed ifKey is not the same type asKeyContainer::value_type.
      Compare - ACompare type providing a strict weak ordering.
      KeyContainer - The type of the underlyingSequenceContainer to store the elements. The iterators of such container should satisfyLegacyRandomAccessIterator or modelrandom_access_iterator.

      The standard containersstd::vector andstd::deque satisfy these requirements.

      [edit]Member types

      Type Definition
      container_typeKeyContainer[edit]
      key_typeKey[edit]
      value_typeKey[edit]
      key_compareCompare[edit]
      value_compareCompare[edit]
      referencevalue_type&[edit]
      const_referenceconst value_type&[edit]
      size_typetypename KeyContainer::size_type[edit]
      difference_typetypename KeyContainer::difference_type[edit]
      iterator implementation-definedLegacyRandomAccessIterator,ConstexprIterator(since C++26) andrandom_access_iterator tovalue_type[edit]
      const_iterator implementation-definedLegacyRandomAccessIterator,ConstexprIterator(since C++26) andrandom_access_iterator toconst value_type[edit]
      reverse_iteratorstd::reverse_iterator<iterator>[edit]
      const_reverse_iteratorstd::reverse_iterator<const_iterator>[edit]

      [edit]Member objects

      Member Description
      container_typec(private) the adapted container
      (exposition-only member object*)
      key_comparecompare(private) the comparison function object
      (exposition-only member object*)

      [edit]Member functions

      constructs theflat_set
      (public member function)[edit]
      (destructor)
      (implicitly declared)
      destroys every element of the container adaptor
      (public member function)
      assigns values to the container adaptor
      (public member function)[edit]
      Iterators
      returns an iterator to the beginning
      (public member function)[edit]
      returns an iterator to the end
      (public member function)[edit]
      returns a reverse iterator to the beginning
      (public member function)[edit]
      returns a reverse iterator to the end
      (public member function)[edit]
      Capacity
      checks whether the container adaptor is empty
      (public member function)[edit]
      returns the number of elements
      (public member function)[edit]
      returns the maximum possible number of elements
      (public member function)[edit]
      Modifiers
      constructs element in-place
      (public member function)[edit]
      constructs elements in-place using a hint
      (public member function)[edit]
      inserts elements
      (public member function)[edit]
      inserts a range of elements
      (public member function)[edit]
      extracts the underlying container
      (public member function)[edit]
      replaces the underlying container
      (public member function)[edit]
      erases elements
      (public member function)[edit]
      swaps the contents
      (public member function)[edit]
      clears the contents
      (public member function)[edit]
      Lookup
      finds element with specific key
      (public member function)[edit]
      returns the number of elements matching specific key
      (public member function)[edit]
      checks if the container contains element with specific key
      (public member function)[edit]
      returns an iterator to the first elementnot less than the given key
      (public member function)[edit]
      returns an iterator to the first elementgreater than the given key
      (public member function)[edit]
      returns range of elements matching a specific key
      (public member function)[edit]
      Observers
      returns the function that compares keys
      (public member function)[edit]
      returns the function that compares keys in objects of typevalue_type
      (public member function)[edit]

      [edit]Non-member functions

      lexicographically compares the values of twoflat_sets
      (function template)[edit]
      specializes thestd::swap algorithm
      (function template)[edit]
      erases all elements satisfying specific criteria
      (function template)[edit]

      [edit]Helper classes

      specializes thestd::uses_allocator type trait
      (class template specialization)[edit]

      [edit]Tags

      indicates that elements of a range are sorted and unique
      (tag)[edit]

      [edit]Deduction guides

      [edit]Notes

      The member typesiterator andconst_iterator may be aliases to the same type. This means defining a pair of function overloads using the two types as parameter types may violate theOne Definition Rule. Sinceiterator is convertible toconst_iterator, a single function with aconst_iterator as parameter type will work instead.

      Some advantages of flat set over other standardcontainer adaptors are:

      • Potentially faster lookup (even though search operations have logarithmic complexity).
      • Much faster iteration:random access iterators instead ofbidirectional iterators.
      • Less memory consumption for small objects (and for big objects ifKeyContainer::shrink_to_fit() is available).
      • Better cache performance (depending onKeyContainer, keys are stored in a contiguous block(s) of memory).

      Some disadvantages of flat set are:

      • Non-stable iterators (iterators are invalidated when inserting and erasing elements).
      • Non-copyable and non-movable type values can not be stored.
      • Weaker exception safety (copy/move constructors can throw when shifting values in erasures and insertions).
      • Slower (i.e., linear) insertion and erasure, especially for non-movable types.
      Feature-test macroValueStdFeature
      __cpp_lib_flat_set202207L(C++23)std::flat_set andstd::flat_multiset
      __cpp_lib_constexpr_flat_set202502L(C++26)constexprstd::flat_set

      [edit]Example

      This section is incomplete
      Reason: no example

      [edit]See also

      adapts a container to provide a collection of keys, sorted by keys
      (class template)[edit]
      collection of unique keys, sorted by keys
      (class template)[edit]
      collection of unique keys, hashed by keys
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/flat_set&oldid=182862"

      [8]ページ先頭

      ©2009-2025 Movatter.jp