Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      C++ named requirements:UnorderedAssociativeContainer(since C++11)

      From cppreference.com
      <cpp‎ |named req
       
       
      C++ named requirements
       

      Unordered associative containers areContainers that provide fast lookup of objects based on keys.Worst case complexity is linear but on average much faster for most of the operations.

      Unordered associative containers are parametrized byKey;Hash, aHash function object which acts as hash function onKey; andPred, aBinaryPredicate evaluating equivalence betweenKeys.std::unordered_map andstd::unordered_multimap also have a mapped typeT associated with theKey.

      If twoKeys are equal according toPred,Hash must return the same value for both keys.

      If bothHash::is_transparent andPred::is_transparent exist and each names a type, member functionsfind,contains,count,equal_range, andbucket accept arguments of types other thanKey and expect thatHash is callable with values of those types, and thatPred is a transparent comparison function such asstd::equal_to<>.

      (since C++20)

      std::unordered_map andstd::unordered_set can contain at most one element with a given key,std::unordered_multiset andstd::unordered_multimap instead can have multiple elements with the same key (which must always be adjacent on iterations).

      Forstd::unordered_set andstd::unordered_multiset the value type is the same as the key type and bothiterator andconst_iterator are constant iterators.Forstd::unordered_map andstd::unordered_multimap the value type isstd::pair<const Key, T>.

      Elements in an unordered associative container are organized into buckets, keys with the same hash will end up in the same bucket.The number of buckets is increased when the size of the container increases to keep the average number of elements in each bucket under a certain value.

      Rehashing invalidates iterator and might cause the elements to be re-arranged in different buckets but it does not invalidate references to the elements.

      Unordered associative containers meet the requirements ofAllocatorAwareContainer.Forstd::unordered_map andstd::unordered_multimap the requirements ofvalue_type inAllocatorAwareContainer apply tokey_type andmapped_type (not tovalue_type).

      Contents

      [edit]Requirements

      Legend
      X An unordered associative container class
      a A value of typeX
      a2 A value of a type withnodes compatible with typeX
      b A value of typeX orconst X
      a_uniq A value of typeX whenX supports unique keys
      a_eq A value of typeX whenX supports equivalent keys
      a_tran A value of typeX orconst X when the qualified identifiersX::key_equal::is_transparent andX::hasher::is_transparent are both valid and denotetypes
      i,j Input iterators that refer tovalue_type
      [ij) A valid range
      rg(since C++23) A value of a typeR that modelscontainer-compatible-range<value_type>
      p,q2 Valid constant iterators toa
      q,q1 Valid dereferenceable constant iterators toa
      r A valid dereferenceable iterator toa
      [q1q2) A valid range ina
      il A value of typestd::initializer_list<value_type>
      t A value of typeX::value_type
      k A value of typekey_type
      hf A value of typehasher orconst hasher
      eq A value of typekey_equal orconst key_equal
      ke A value such that
      • eq(r1, ke)== eq(ke, r1),
      • hf(r1)== hf(ke) ifeq(r1, ke) istrue, and
      • if any two ofeq(r1, ke),eq(r2, ke), andeq(r1, r2) aretrue, then all three aretrue,

      wherer1 andr2 are keys of elements ina_tran

      kx(since C++23) A value such that
      • eq(r1, kx)== eq(kx, r1),
      • hf(r1)== hf(kx) ifeq(r1, kx) istrue,
      • if any two ofeq(r1, kx),eq(r2, kx), andeq(r1, r2) aretrue, then all three aretrue, and
      • kx is not convertible to eitheriterator orconst_iterator,

      wherer1 andr2 are keys of elements ina_tran

      n A value of typesize_type
      z A value of typefloat
      nh(since C++17) An rvalue of typeX::node_type

      [edit]Member types

      NameTypeRequirementsNotes
      X::key_typeKey
      X::mapped_typeTstd::unordered_map andstd::unordered_multimap only
      X::value_typeKeystd::unordered_set andstd::unordered_multiset only.Erasable inX
      std::pair<const Key, T>std::unordered_map andstd::unordered_multimap only.Erasable inX
      X::hasherHashHash
      X::key_equalPredCopyConstructible;BinaryPredicate that takes two arguments of typeKey and expresses an equivalence relation
      X::local_iteratorLegacyIteratorCategory and types are the same asX::iteratorCan be used to iterate through a single bucket, but not across buckets
      X::const_local_iteratorLegacyIteratorCategory and types are the same asX::const_iterator
      X::node_type(since C++17)A specialization ofnode-handle class templateThe public nested types are the same as the corresponding types inX

      [edit]Member functions and operators

      ExpressionResultPreconditionsEffectsReturnsComplexity
      X(n, hf, eq)Constructs an empty container with at leastn buckets, usinghf as the hash function andeq as the key equality predicateO(n)
      X(n, hf)key_equal isDefaultConstructibleConstructs an empty container with at leastn buckets, usinghf as the hash function andkey_equal() as the key equality predicateO(n)
      X(n)hasher andkey_equal areDefaultConstructibleConstructs an empty container with at leastn buckets, usinghasher() as the hash function andkey_equal() as the key equality predicateO(n)
      X a= X();
      X a;
      hasher andkey_equal areDefaultConstructibleConstructs an empty container with an unspecified number of buckets, usinghasher() as the hash function andkey_equal() as the key equality predicateConstant
      X(i, j, n, hf, eq)value_type isEmplaceConstructible intoX from*iConstructs an empty container with at leastn buckets, usinghf as the hash function andeq as the key equality predicate, and inserts elements from[ij) into itAverage caseO(N) (N isstd::distance(i, j)), worst caseO(N2)
      X(i, j, n, hf)key_equal is theDefaultConstructible.value_type isEmplaceConstructible intoX from*iConstructs an empty container with at leastn buckets, usinghf as the hash function andkey_equal() as the key equality predicate, and inserts elements from[ij) into itAverage caseO(N) (N isstd::distance(i, j)), worst caseO(N2)
      X(i, j, n)hasher andkey_equal areDefaultConstructible.value_type isEmplaceConstructible intoX from*iConstructs an empty container with at leastn buckets, usinghasher() as the hash function andkey_equal() as the key equality predicate, and inserts elements from[ij) into itAverage caseO(N) (N isstd::distance(i, j)), worst caseO(N2)
      X(i, j)hasher andkey_equal areDefaultConstructible.value_type isEmplaceConstructible intoX from*iConstructs an empty container with an unspecified number of buckets, usinghasher() as the hash function andkey_equal() as the key equality predicate, and inserts elements from[ij) into itAverage caseO(N) (N isstd::distance(i, j)), worst caseO(N2)
      X(std::from_range,
        rg, n, hf, eq)

      (since C++23)
      value_type isEmplaceConstructible intoX from*ranges::begin(rg)Constructs an empty container with at leastn buckets, usinghf as the hash function andeq as the key equality predicate, and inserts elements fromrg into itAverage caseO(N) (N isranges::distance(rg)), worst caseO(N2)
      X(std::from_range,
        rg, n, hf)

      (since C++23)
      key_equal isDefaultConstructible.value_type isEmplaceConstructible intoX from*ranges::begin(rg)Constructs an empty container with at leastn buckets, usinghf as the hash function andkey_equal() as the key equality predicate, and inserts elements fromrg into itAverage caseO(N) (N isranges::distance(rg)), worst caseO(N2)
      X(std::from_range,
        rg, n)

      (since C++23)
      hasher andkey_equal areDefaultConstructible.value_type isEmplaceConstructible intoX from*ranges::begin(rg)Constructs an empty container with at leastn buckets, usinghasher() as the hash function andkey_equal() as the key equality predicate, and inserts elements fromrg into itAverage caseO(N) (N isranges::distance(rg)), worst caseO(N2)
      X(std::from_range,
        rg)

      (since C++23)
      hasher andkey_equal areDefaultConstructible.value_type isEmplaceConstructible intoX from*ranges::begin(rg)Constructs an empty container with an unspecified number of buckets, usinghasher() as the hash function andkey_equal() as the key equality predicate, and inserts elements fromrg into itAverage caseO(N) (N isranges::distance(rg)), worst caseO(N2)
      X(il)X(il.begin(), il.end())
      X(il, n)X(il.begin(), il.end(), n)
      X(il, n, hf)X(il.begin(), il.end(), n, hf)
      X(il, n, hf, eq)X(il.begin(), il.end(), n, hf, eq)
      X(b)Container; Copies the hash function, predicate, and maximum load factorAverage case linear inb.size(), worst caseO(N2)
      a= bX&Container; copies the hash function, predicate, and maximum load factorAverage case linear inb.size(), worst caseO(N2)
      a= ilX&value_type isCopyInsertable intoX andCopyAssignableAssigns the range[il.begin()il.end()) intoa. All existing elements ofa are either assigned to or destroyedAverage case linear inil.size(), worst caseO(N2)
      b.hash_function()hasherb's hash functionConstant
      b.key_eq()key_equalb's key equality predicateConstant
      a_uniq.emplace(args)std::pair<
        iterator,
       bool>
      value_type isEmplaceConstructible intoX fromargsInserts avalue_type objectt constructed withstd::forward<Args>(args)... if and only if there is no element in the container with key equivalent to the key oftThebool component of the returned pair istrue if and only if the insertion takes place, and the iterator component of the pair points to the element with key equivalent to the key oftAverage caseO(1), worst caseO(a_uniq.size())
      a_eq.emplace(args)iteratorvalue_type isEmplaceConstructible intoX fromargsInserts avalue_type objectt constructed withstd::forward<Args>(args)...An iterator pointing to the newly inserted elementAverage caseO(1), worst caseO(a_eq.size())
      a.emplace_hint(p, args)iteratorvalue_type isEmplaceConstructible intoX fromargsa.emplace(
       std::forward<Args>(args)...)
      An iterator pointing to the element with the key equivalent to the newly inserted element. Theconst_iteratorp is a hint pointing to where the search should start. Implementations are permitted to ignore the hintAverage caseO(1), worst caseO(a.size())
      a_uniq.insert(t)std::pair<
        iterator,
       bool>
      Ift is a non-const rvalue,value_type isMoveInsertable intoX; otherwise,value_type isCopyInsertable intoXInsertst if and only if there is no element in the container with key equivalent to the key oftThebool component of the returned pair indicates whether the insertion takes place, and theiterator component points to the element with key equivalent to the key oftAverage caseO(1), worst caseO(a_uniq.size())
      a_eq.insert(t)iteratorIft is a non-const rvalue,value_type isMoveInsertable intoX; otherwise,value_type isCopyInsertable intoXInsertstAn iterator pointing to the newly inserted elementAverage caseO(1), worst caseO(a_eq.size())
      a.insert(p, t)iteratorIft is a non-const rvalue,value_type isMoveInsertable intoX; otherwise,value_type isCopyInsertable intoXa.insert(t). The iteratorp is a hint pointing to where the search should start. Implementations are permitted to ignore the hintAn iterator pointing to the element with the key equivalent to that oftAverage caseO(1), worst caseO(a.size())
      a.insert(i, j)voidvalue_type isEmplaceConstructible intoX from*i. Neitheri norj are iterators intoaa.insert(t) for each element in
      [ij)
      Average caseO(N), whereN isstd::distance(i, j), worst caseO((a.size()+1))
      a.insert_range(rg)
      (since C++23)
      voidvalue_type isEmplaceConstructible intoX from*ranges::begin(rg).rg anda do not overlapa.insert(t) for each elementt inrgAverage caseO(N), whereN isranges::distance(rg), worst caseO((a.size()+1))
      a.insert(il)a.insert(il.begin(), il.end())
      a_uniq.insert(nh)
      (since C++17)
      insert_return_typenh is empty or

      a_uniq.get_allocator()
      ==
      nh.get_allocator()
      istrue

      Ifnh is empty, has no effect. Otherwise, inserts the element owned bynh if and only if there is no element in the container with a key equivalent tonh.key(). Ensures: Ifnh is empty,inserted isfalse,position isend(), andnode is empty. Otherwise if the insertion took place,inserted istrue,position points to the inserted element, andnode is empty; if the insertion failed,inserted isfalse,node has the previous value ofnh, andposition points to an element with a key equivalent tonh.key()Average caseO(1), worst caseO(a_uniq.size())
      a_eq.insert(nh)
      (since C++17)
      iteratornh is empty or

      a_eq.get_allocator()
      ==
      nh.get_allocator()
      istrue

      Ifnh is empty, has no effect and returnsa_eq.end(). Otherwise, inserts the element owned bynh and returns an iterator pointing to the newly inserted element. Ensures:nh is emptyAverage caseO(1), worst caseO(a_eq.size())
      a.insert(q, nh)
      (since C++17)
      iteratornh is empty or

      a.get_allocator()
      ==
      nh.get_allocator()
      istrue

      Ifnh is empty, has no effect and returnsa.end(). Otherwise, inserts the element owned bynh if and only if there is no element with key equivalent tonh.key() in containers with unique keys; always inserts the element owned bynh in containers with equivalent keys. The iteratorq is a hint pointing to where the search should start. Implementations are permitted to ignore the hint. Ensures:nh is empty if insertion succeeds, unchanged if insertion failsAn iterator pointing to the element with key equivalent tonh.key()Average caseO(1), worst caseO(a.size())
      a.extract(k)
      (since C++17)
      node_typeRemoves an element in the container with key equivalent tokAnode_type owning the element if found, otherwise an emptynode_typeAverage caseO(1), worst caseO(a.size())
      a_tran.extract(kx)
      (since C++23)
      node_typeRemoves an element in the container with key equivalent tokxAnode_type owning the element if found, otherwise an emptynode_typeAverage caseO(1), worst caseO(a_tran.size())
      a.extract(q)
      (since C++17)
      node_typeRemoves the element pointed to byqAnode_type owning that elementAverage caseO(1), worst caseO(a.size())
      a.merge(a2)
      (since C++17)
      voida.get_allocator()
      ==
      a2.get_allocator()
      Attempts to extract each element ina2 and insert it intoa using the hash function and key equality predicate ofa. In containers with unique keys, if there is an element ina with key equivalent to the key of an element froma2, then that element is not extracted froma2. Ensures: Pointers and references to the transferred elements ofa2 refer to those same elements but as members ofa. Iterators referring to the transferred elements and all iterators referring toa will be invalidated, but iterators to elements remaining ina2 will remain validAverage caseO(N), whereN isa2.size(), worst caseO((a.size()+1))
      a.erase(k)size_typeErases all elements with key equivalent tokThe number of elements erasedAverage caseO(a.count(k)), worst caseO(a.size())
      a_tran.erase(kx)
      (since C++23)
      size_typeErases all elements with key equivalent tokxThe number of elements erasedAverage caseO(a_tran.count(kx)), worst caseO(a_tran.size())
      a.erase(q)iteratorErases the element pointed to byqThe iterator immediately followingq prior to the erasureAverage caseO(1), worst caseO(a.size())
      a.erase(r)
      (since C++17)
      iteratorErases the element pointed to byrThe iterator immediately followingr prior to the erasureAverage caseO(1), worst caseO(a.size())
      a.erase(q1, q2)iteratorErases all elements in the range
      [q1q2)
      The iterator immediately following the erased elements prior to the erasureAverage case linear instd::distance(q1, q2), worst caseO(a.size())
      a.clear()voidErases all elements in the container. Ensures:a.empty() istrueLinear ina.size()
      b.find(k)iterator;const_iterator for constantbAn iterator pointing to an element with key equivalent tok, orb.end() if no such element existsAverage caseO(1), worst caseO(b.size())
      a_tran.find(ke)
      (since C++17)?
      iterator;const_iterator for constanta_tranAn iterator pointing to an element with key equivalent toke, ora_tran.end() if no such element existsAverage caseO(1), worst caseO(a_tran.size())
      b.count(k)size_typeThe number of elements with key equivalent tokAverage caseO(b.count(k)), worst caseO(b.size())
      a_tran.count(ke)
      (since C++17)?
      size_typeThe number of elements with key equivalent tokeAverage caseO(a_tran.count(ke)), worst caseO(a_tran.size())
      b.contains(k)
      (since C++20)?
      b.find(k)!= b.end()
      a_tran.contains(ke)
      (since C++20)?
      a_tran.find(ke)!= a_tran.end()
      b.equal_range(k)std::pair<
        iterator,
        iterator>;

      std::pair<
        const_iterator,
        const_iterator> for constantb

      A range containing all elements with keys equivalent tok. Returns

      std::make_pair(
        b.end(), b.end())
      if no such elements exist

      Average caseO(b.count(k)), worst caseO(b.size())
      a_tran.equal_range(ke)
      (since C++20)?
      std::pair<
        iterator,
        iterator>;

      std::pair<
        const_iterator,
        const_iterator>for constanta_tran

      A range containing all elements with keys equivalent toke. Returns

      std::make_pair(
        a_tran.end(),
        a_tran.end())
      if no such elements exist

      Average caseO(a_tran.count(ke)), worst caseO(a_tran.size())
      b.bucket_count()size_typeThe number of buckets thatb containsConstant
      b.max_bucket_count()size_typeAn upper bound on the number of buckets thatb can ever containConstant
      b.bucket(k)size_typeb.bucket_count()>0The index of the bucket in which elements with keys equivalent tok would be found, if any such element existed. The return value is in[0b.bucket_count())Constant
      a_tran.bucket(ke)size_typea_tran.
      bucket_count()>0
      The index of the bucket in which elements with keys equivalent toke would be found, if any such element existed. The return value must be in the range[0a_tran.bucket_count())Constant
      b.bucket_size(n)size_typen is in[0b.bucket_count())The number of elements in thenth bucketO(b.bucket_size(n))
      b.begin(n)local_iterator;const_local_iterator for constantbn is in[0b.bucket_count())An iterator referring to the first element in the bucket. If the bucket is empty, thenb.begin(n)== b.end(n)Constant
      b.end(n)local_iterator;const_local_iterator for constantbn is in[0b.bucket_count())An iterator which is the past-the-end value for the bucketConstant
      b.cbegin(n)const_local_iteratorn is in[0b.bucket_count())An iterator referring to the first element in the bucket. If the bucket is empty, thenb.cbegin(n)== b.cend(n)Constant
      b.cend(n)const_local_iteratorn is in[0b.bucket_count())An iterator which is the past-the-end value for the bucketConstant
      b.load_factor()floatThe average number of elements per bucketConstant
      b.max_load_factor()floatA positive number that the container attempts to keep the load factor less than or equal to. The container automatically increases the number of buckets as necessary to keep the load factor below this numberConstant
      a.max_load_factor(z)voidz is positive. May change the container's maximum load factor, usingz as a hintConstant
      a.rehash(n)voidEnsures:

      a.bucket_count()>=
        a.size()/ a.max_load_factor()
      anda.bucket_count()>= n

      Average case linear ina.size(), worst caseO(N2)
      a.reserve(n)a.rehash(std::ceil(
        n/ a.max_load_factor()))
      This section is incomplete
      Reason: Requirements regarding member functions.

      [edit]Standard library

      The following standard library containers satisfy theUnorderedAssociativeContainer requirements:

      collection of unique keys, hashed by keys
      (class template)[edit]
      collection of keys, hashed by keys
      (class template)[edit]
      collection of key-value pairs, hashed by keys, keys are unique
      (class template)[edit]
      collection of key-value pairs, hashed by keys
      (class template)[edit]

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2156C++11the load factor after rehashing could only be
      strictly lower than the maximum load factor
      allowed to be equal
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/named_req/UnorderedAssociativeContainer&oldid=177778"

      [8]ページ先頭

      ©2009-2025 Movatter.jp