Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      C++ named requirements:Container

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

      AContainer is an object used to store other objects and taking care of the management of the memory used by the objects it contains.

      Contents

      [edit]Requirements

      Given the following types and values:

      Type Definition
      T an object type
      C a container class containing objects of typeT
      Value Definition
      u,v values of typeC orconst C
      mv a value of typeC
      cv a value of typeconst C
      lhs,rhs lvalues of typeC
      i,j values of typeC::iterator orconst C::iterator

      C satisfies the requirements ofContainer if the following types, statements, and expressions are well-formed and have the specified semantics:

      [edit]Types

      TypeDefinitionRequirements
      typename C::value_typeTT isCopyConstructible(until C++11)Erasable fromC(since C++11).
      typename C::referenceT&No explicit requirement
      typename C::const_referenceconst T&
      typename C::iteratoran iterator type
      typename C::const_iteratora constant iterator typeC::const_iterator is aLegacyForwardIterator, and itsvalue type isT.
      typename C::difference_typea signed integer typeC::difference_type is the same as thedifference type ofC::iterator andC::const_iterator.
      typename C::size_typean unsigned integer typeC::size_type is large enough to represent all non-negative values ofC::difference_type.

      [edit]Statements

      StatementSemantics Complexity 
      C c;

      C c= C();

      Postcondition c.empty() istrue.constant
      C c(v);

      C c= C(v);

      Precondition

      Ifv is not an rvalue of typeC,T isCopyInsertable intoC.

      (since C++11)
      linear[1]
      Postcondition
      • Ifv is an lvalue,c== v istrue.
      • Ifv is an rvalue, andc andv do not refer to the same object(since C++11),c is equal to the value thatv had before this construction.
      Notes
      1. Ifv is an rvalue of typeC, andC is not a specialization ofstd::array orstd::inplace_vector, the complexity is constant.

      [edit]Expressions

      ExpressionTypeSemantics Complexity 
      C()CPostcondition C().empty() istrue.constant
      C(v)CPrecondition

      Ifv is not an rvalue of typeC,T isCopyInsertable intoC.

      (since C++11)
      constant[1]
      Postcondition
      • Ifv is an lvalue,C(v)== v istrue.
      • Ifv is an rvalue, andC(v) andv do not refer to the same object(since C++11),C(v) is equal to the value thatv had before this construction.
      lhs= vC&Postcondition
      • Ifv is an lvalue,lhs== v istrue.
      • Ifv is an rvalue, andlv andv do not refer to the same object(since C++11),lhs is equal to the value thatv had before this assignment.
      linear
      v.~C()voidEffectDestroys all elements ofv and deallocates all memory obtained.linear
      mv.begin()C::iteratorEffectReturns an iterator pointing to the first element ofmv.constant
      cv.begin()C::const_iteratorEffectReturns an iterator pointing to the first element ofcv.constant
      mv.end()C::iteratorEffectReturns the past-the-end iterator ofmv.constant
      cv.end()C::const_iteratorEffectReturns the past-the-end iterator ofcv.constant
      v.cbegin()
      (since C++11)
      C::const_iteratorEffectReturnsconst_cast<const C&>(v).begin().constant
      v.cend()
      (since C++11)
      C::const_iteratorEffectReturnsconst_cast<const C&>(v).end().constant
      i<=> j
      (since C++20)
      std::strong_ordering    ConstraintThis expression is only required to be well-formed ifC::iterator satisfies the random access iterator requirements.constant
      u== vboolEffectReturns
      u.size()== v.size()&&
         std::equal(u.begin(),
               u.end(), v.begin())
      (until C++14)
      std::equal(u.begin(), u.end(),
                 v.begin(), v.end())
      (since C++14)
      .
      linear[2]
      u!= vEffectEquivalent to!(u== v).
      lhs.swap(rhs)

      swap(lhs, rhs)

      voidEffectExchanges the contents oflhs andrhs.constant[3]
      v.size()C::size_typeEffectReturns the number of elements[4] ofv.constant
      v.max_size()C::size_typeEffectReturns the number of elements of the largest possible container of typeC.constant
      v.empty()boolEffectReturnsv.begin()== v.end().constant
      Optional container requirements
      (only provided for some types of containers)
      u<=> v
      (since C++20)
      synth-three-way-result
          <C::value_type>
      PreconditionEitherT modelsthree_way_comparable, oroperator< is a total ordering relationship defined for values of typeT andconst T.linear
      EffectReturnsstd::lexicographical_compare_three_way
          (u.begin(), u.end(),
           v.begin(), v.end(),
           synth-three-way )
      [5].
      Notes
      1. Ifv is an rvalue of typeC, andC is a specialization ofstd::array orstd::inplace_vector, the complexity is linear.
      2. Ifu.size()!= v.size() istrue, the complexity is constant.
      3. IfC is a specialization ofstd::array orstd::inplace_vector, the complexity is linear.
      4. The number of elements is defined by the rules of constructors, inserts, and erases. It is equal to the value ofstd::distance(v.begin(), v.end()).
      5. If the iterators passed tostd::lexicographical_compare_three_way areConstexprIterators, the operation is implemented byconstexpr functions.

      In the expressionsi== j,i!= j,i< j,i<= j,i>= j,i> j andi- j, ifi and/orj are replaced by iterators of typeC::const_iterator pointing to the same element respectively, the semantics remain the same.

      [edit]Container data races

      Seecontainer thread safety.

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 179C++98iterator andconst_iterator types might be incomparablerequired to be comparable
      LWG 276C++98T was required to beCopyAssignableT is required to be
      CopyConstructible
      LWG 322C++98the value types ofiterator andconst_iterator were not specifiedspecified asT
      LWG 774C++98there was no requirement onswap(a, b)added
      LWG 883C++98a.swap(b) was defined asswap(a, b),
      resulted in circular definition
      defined as exchanging
      the values ofa andb
      LWG 1319C++98iterator andconst_iterator
      might not have multipass guarantee
      they are required to satisfy
      the requirements of
      LegacyForwardIterator
      LWG 2114
      (P2167R3)
      C++98non-bool return types of some functions were alloweddisallowed
      LWG 2182C++98the types deonted byreference and
      const_reference were poorly specified
      improved wording
      LWG 2257C++98two containers required linear time to compare
      equal even if they have different sizes
      only requires constant
      time in this case
      LWG 2263C++11the resolution ofLWG issue 179 was accidentally dropped in C++11restored
      LWG 2839C++11self move assignment of standard containers was not allowedallowed but the
      result is unspecified
      N3346C++11C::value_type was required to beDestructiblerequired to beErasable fromC

      [edit]See also

      C++ documentation forContainers library
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/named_req/Container&oldid=177547"

      [8]ページ先頭

      ©2009-2025 Movatter.jp