Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      C++ named requirements:AssociativeContainer

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

      AnAssociativeContainer is an orderedContainer that provides fast lookup of objects based on keys.

      An associative container supportsunique keys if it may contain at most one element for each key. Otherwise, it supportsequivalent keys.

      Contents

      [edit]Requirements

      Legend
      X An associative container class
      T The element type ofX
      A The allocator type ofX:X::allocator_type if it exists, otherwisestd::allocator<X::value_type>
      a A value of typeX
      a2 A value of a typeY whosenode handles are compatible withX
      b A value of typeX orconst X
      u A name of a variable beging declared
      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 typeX::key_compare::is_transparent exists
      i,j TheLegacyInputIterators referring to elements implicitly convertible toX::value_type
      [ij) A valid range
      rg
      (since C++23)
      A value of a typeR that modelscontainer-compatible-range<value_type>
      p A valid constant iterator toa
      q A valid dereferenceable constant iterator toa
      r A valid dereferenceable iterator toa
      q1,q2 A valid range of const iterators ina
      il An object of typestd::initializer_list<X::value_type>
      t A value of typeX::value_type
      k A value of typeX::key_type
      c A value of typeX::key_compare orconst X::key_compare
      kl A value such thata is partitioned with respect toc(x, kl), withx the key value ofe ande ina
      ku A value such thata is partitioned with respect to!c(ku, x), withx the key value ofe ande ina
      ke A value such thata is partitioned with respect toc(x, ke) and!c(ke, x), withc(x, ke) implying!c(ke, x) and withx the key value ofe ande ina
      kx
      (since C++23)
      A value such that:
      • a is partitioned with respect toc(x, kx) and!c(kx, x), withc(x, kx) implying!c(kx, x) and withx the key value ofe ande ina, and
      • kx is not convertible to eitherX::iterator orX::const_iterator
      m An allocator of a type convertible toA
      nh A non-const rvalue of typeX::node_type

      The typeX satisfiesAssociativeContainer if

      • The typeX satisfiesContainer(until C++11)AllocatorAwareContainer(since C++11),
      • Is parameterized onKey and an ordering relationCompare that induces astrict weak ordering on elements ofKey, and
        • In addition,std::map andstd::multimap associate an arbitrarymapped typeT with theKey.
        • The object of typeCompare is called thecomparison object of a container of typeX.
      • The following expressions must be valid and have their specified effects for all associative containers:

      [edit]Types

      NameTypeRequirements
      key_typeKey
      mapped_typeT (forstd::map andstd::multimap only)
      value_typeErasable fromX
      key_compareCompareCopyConstructible
      value_compareBinaryPredicate
      node_typeA specialization of thenode-handle class template, such that the public nested types are the same types as the corresponding types inX.

      [edit]Member functions and operators

      ExpressionResultPreconditionsEffectsReturnsComplexity
      X(c)Constructs an empty container. Uses a copy ofc as a comparison objectConstant
      X u= X();
      X u;
      key_compare meets theDefaultConstructible requirementsConstructs an empty container. UsesCompare() as a comparison objectConstant
      X(i, j, c)value_type isEmplaceConstructible intoX from*iConstructs an empty container and inserts elements from the range[ij) into it; usesc as a comparison objectN·log(N) in general, whereN has the valuestd::distance(i, j); linear if[ij) is sorted with respect tovalue_comp()
      X(i, j)key_compare meets theDefaultConstructible requirements.value_type isEmplaceConstructible intoX from*iConstructs an empty container and inserts elements from the range[ij) into it; usesCompare() as a comparison object
      X(from_range, rg, c)
      (since C++23)
      value_type isEmplaceConstructible intoX from*ranges::begin(rg)Constructs an empty container and inserts each element fromrg into it. Usesc as the comparison objectN·log(N) in general, whereN has the valueranges::distance(rg); linear ifrg is sorted with respect tovalue_comp()
      X(from_range, rg)
      (since C++23)
      key_compare meets theDefaultConstructible requirements.value_type isEmplaceConstructible intoX from*ranges::begin(rg)Constructs an empty container and inserts each element fromrg into it. UsesCompare() as the comparison object
      X(il, c)X(il.begin(), il.end(), c)
      X(il)X(il.begin(), il.end())
      a= ilX&value_type isCopyInsertable intoX andCopyAssignableAssigns the range[il.begin()il.end()) intoa. All existing elements ofa are either assigned to or destroyedN·log(N) in general, whereN has the valueil.size()+ a.size(); linear if[il.begin()il.end()) is sorted with respect tovalue_comp()
      b.key_comp()X::key_compareThe comparison object out of whichb was constructedConstant
      b.value_comp()X::value_compareAn object ofvalue_compare constructed out of the comparison objectConstant
      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 oftLogarithmic
      a_eq.emplace(args)iteratorvalue_type isEmplaceConstructible intoX fromargsInserts avalue_type objectt constructed withstd::forward<Args>(args).... If a range containing elements equivalent tot exists ina_eq,t is inserted at the end of that rangeAn iterator pointing to the newly inserted elementLogarithmic
      a.emplace_hint(p, args)iteratorEquivalent to

      a.emplace(
       std::forward<Args>(args)...)
      ,except that the element is inserted as close as possible to the position just prior top

      An iterator pointing to the element with the key equivalent to the newly inserted elementLogarithmic in general, but amortized constant if the element is inserted right beforep
      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 istrue if and only if the insertion takes place, and theiterator component of the pair points to the element with key equivalent to the key oftLogarithmic
      a_eq.insert(t)iteratorIft is a non-const rvalue,value_type isMoveInsertable intoX; otherwise,value_type isCopyInsertable intoXInsertst and returns the iterator pointing to the newly inserted element. If a range containing elements equivalent tot exists ina_eq,t is inserted at the end of that rangeLogarithmic
      a.insert(p, t)iteratorIft is a non-const rvalue,value_type isMoveInsertable intoX; otherwise,value_type isCopyInsertable intoXInsertst if and only if there is no element with key equivalent to the key oft in containers with unique keys; always insertst in containers with equivalent keys.t is inserted as close as possible to the position just prior topAn iterator pointing to the element with key equivalent to the key oftLogarithmic in general, but amortized constant ift is inserted right beforep
      a.insert(i, j)voidvalue_type isEmplaceConstructible intoX from*i. Neitheri norj are iterators intoaInserts each element from the range[ij) if and only if there is no element with key equivalent to the key of that element in containers with unique keys; always inserts that element in containers with equivalent keysN·log(a.size()+ N), whereN has the valuestd::distance(i, j)
      a.insert_range(rg)
      (since C++23)
      voidvalue_type isEmplaceConstructible intoX from*ranges::begin(rg).rg anda do not overlapInserts each element fromrg if and only if there is no element with key equivalent to the key of that element in containers with unique keys; always inserts that element in containers with equivalent keysN·log(a.size()+ N), whereN has the valueranges::distance(rg)
      a.insert(il)a.insert(il.begin(), il.end())
      a_uniq.insert(nh)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()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()Logarithmic
      a_eq.insert(nh)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. If a range containing elements with keys equivalent tonh.key() exists ina_eq, the element is inserted at the end of that range. Ensures:nh is emptyLogarithmic
      a.insert(p, nh)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 element is inserted as close as possible to the position just prior top. Ensures:nh is empty if insertion succeeds, unchanged if insertion failsAn iterator pointing to the element with key equivalent tonh.key()Logarithmic in general, but amortized constant if the element is inserted right beforep
      a.extract(k)node_typeRemoves the first element in the container with key equivalent tokAnode_type owning the element if found, otherwise an emptynode_typelog(a.size())
      a_tran.extract(kx)
      (since C++23)
      node_typeRemoves the first element in the container with keyr such that!c(r, kx)&&!c(kx, r) istrueAnode_type owning the element if found, otherwise an emptynode_typelog(a_tran.size())
      a.extract(q)node_typeRemoves the element pointed to byqAnode_type owning that elementAmortized constant
      a.merge(a2)voida.get_allocator()
      ==
      a2.get_allocator()
      Attempts to extract each element ina2 and insert it intoa using the comparison object 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 will continue to refer to their elements, but they now behave as iterators intoa, not intoa2. Throws: Nothing unless the comparison object throwsN·log(a.size()+ N), whereN has the valuea2.size()
      a.erase(k)size_typeErases all elements in the container with key equivalent tokThe number of erased elementslog(a.size())
      + a.count(k)
      a_tran.erase(kx)
      (since C++23)
      size_typeErases all elements in the container with keyr such that!c(r, kx)&&!c(kx, r) istrueThe number of erased elementslog(a_tran.size())
      + a_tran.count(kx)
      a.erase(q)iteratorErases the element pointed to byqAn iterator pointing to the element immediately followingq prior to the element being erased. If no such element exists, returnsa.end()Amortized constant
      a.erase(r)iteratorErases the element pointed to byrAn iterator pointing to the element immediately followingr prior to the element being erased. If no such element exists, returnsa.end()Amortized constant
      a.erase(q1, q2)iteratorErases all the elements in the range
      [q1q2)
      An iterator pointing to the element pointed to byq2 prior to any elements being erased. If no such element exists,a.end() is returnedlog(a.size())+ N, whereN has the valuestd::distance(q1, q2)
      a.clear()a.erase(a.begin(), a.end()). Ensures:a.empty() istrueLinear ina.size()
      b.find(k)iterator;const_iterator for constantbAn iterator pointing to an element with the key equivalent tok, orb.end() if such an element is not foundLogarithmic
      a_tran.find(ke)iterator;const_iterator for constanta_tranAn iterator pointing to an element with keyr such that

      !c(r, ke)&&
      !c(ke, r)
      istrue, ora_tran.end() if such an element is not found

      Logarithmic
      b.count(k)size_typeThe number of elements with key equivalent toklog(b.size())
      + b.count(k)
      a_tran.count(ke)size_typeThe number of elements with keyr such that

      !c(r, ke)&&
      !c(ke, r)

      log(a_tran.size())
      + a_tran.count(ke)
      b.contains(k)boolreturn b.find(k)!= b.end();
      a_tran.contains(ke)bool

      return
        a_tran.find(ke)!=
        a_tran.end();

      b.lower_bound(k)iterator;const_iterator for constantbAn iterator pointing to the first element with key not less thank, orb.end() if such an element is not foundLogarithmic
      a_tran.lower_bound(kl)iterator;const_iterator for constanta_tranAn iterator pointing to the first element with keyr such that!c(r, kl), ora_tran.end() if such an element is not foundLogarithmic
      b.upper_bound(k)iterator;const_iterator for constantbAn iterator pointing to the first element with key greater thank, orb.end() if such an element is not foundLogarithmic
      a_tran.upper_bound(ku)iterator;const_iterator for constanta_tranAn iterator pointing to the first element with keyr such thatc(ku, r), ora_tran.end() if such an element is not foundLogarithmic
      b.equal_range(k)std::pair<
        iterator,
        iterator>
      ;

      std::pair<
        const_iterator,
        const_iterator>
      for constantb

      Equivalent to:

      return
       std::make_pair(
          b.lower_bound(k),
          b.upper_bound(k));

      Logarithmic
      a_tran.equal_range(ke)std::pair<
        iterator,
        iterator>
      ;

      std::pair<
        const_iterator,
        const_iterator>
      for constanta_tran

      Equivalent to:

      return
       std::make_pair(
          a_tran.lower_bound(ke),
          a_tran.upper_bound(ke));

      Logarithmic

      [edit]Iterators

      Iterators of associative containers satisfy the requirements ofLegacyBidirectionalIterator.

      For associative containers wherevalue_type is the same askey_type, bothiterator andconst_iterator are constant iterators. It is unspecified whether or notiterator andconst_iterator are the same type.

      Iterators of associative containers iterate through the containers in the non-descending order of keys where non-descending is defined by the comparison that was used to construct the containers. That is, given

      • a, an associative container
      • i andj, dereferenceable iterators toa.

      If the distance fromi toj is positive, thena.value_comp()(*j,*i)==false. Additionally, ifa is an associative container with unique keys, the stronger conditiona.value_comp()(*i,*j)!=false holds.

      This section is incomplete
      Reason: Finish requirements.

      [edit]Standard library

      The following standard library containers satisfy theAssociativeContainer requirements:

      collection of unique keys, sorted by keys
      (class template)[edit]
      collection of keys, sorted by keys
      (class template)[edit]
      collection of key-value pairs, sorted by keys, keys are unique
      (class template)[edit]
      collection of key-value pairs, sorted 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 354C++98lower_bound andupper_bound did not
      return the end iterator if no element is found
      they return the end
      iterator in this case
      LWG 589C++98the elements thati andj refer
      to had the typeX::value_type
      the elements are implicitly
      convertible toX::value_type
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/named_req/AssociativeContainer&oldid=177963"

      [8]ページ先頭

      ©2009-2025 Movatter.jp