Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Iterator library

      From cppreference.com
      <cpp
       
       
      Iterator library
      Iterator concepts
      Iterator primitives
      Algorithm concepts and utilities
      Indirect callable concepts
      Common algorithm requirements
      (C++20)
      (C++20)
      (C++20)
      Utilities
      (C++20)
      Iterator adaptors
      Range access
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++17)(C++20)
      (C++17)
      (C++17)
       

      Iterators are a generalization ofpointers that allow a C++ program to work with different data structures (for example,containers andranges(since C++20)) in a uniform manner. The iterator library provides definitions for iterators, as well as iterator traits, adaptors, and utility functions.

      Since iterators are an abstraction of pointers, their semantics are a generalization of most of the semantics of pointers in C++. This ensures that everyfunction template that takes iterators works as well with regular pointers.

      Contents

      [edit]Iterator categories

      There arefive(until C++17)six(since C++17) kinds of iterators:LegacyInputIterator,LegacyOutputIterator,LegacyForwardIterator,LegacyBidirectionalIterator,LegacyRandomAccessIterator, andLegacyContiguousIterator(since C++17). (See alsoLegacyIterator for the most basic kind of iterator.)

      Instead of being defined by specific types, each category of iterator is defined by the operations that can be performed on it. This definition means that any type that supports the necessary operations can be used as an iterator -- for example, a pointer supports all of the operations required byLegacyRandomAccessIterator, so a pointer can be used anywhere aLegacyRandomAccessIterator is expected.

      All of the iterator categories (exceptLegacyOutputIterator) can be organized into a hierarchy, where more powerful iterator categories (e.g.LegacyRandomAccessIterator) support the operations of less powerful categories (e.g.LegacyInputIterator). If an iterator falls into one of these categories and also satisfies the requirements ofLegacyOutputIterator, then it is called amutable iterator and supportsboth input and output. Non-mutable iterators are calledconstant iterators.

      Iterators are calledconstexpr iterators if all operations provided to meet iterator category requirements areconstexpr functions.

      (since C++20)
      Iterator categoryOperations and storage requirement
           write          read     increment decrement    random   
      access
       contiguous 
      storage
      without
         multiple   
      passes
      with
         multiple   
      passes
      LegacyIteratorRequired
      LegacyOutputIteratorRequiredRequired
      LegacyInputIterator
      (mutable if supports write operation)
      RequiredRequired
      LegacyForwardIterator
      (also satisfiesLegacyInputIterator)
      RequiredRequiredRequired
      LegacyBidirectionalIterator
      (also satisfiesLegacyForwardIterator)
      RequiredRequiredRequiredRequired
      LegacyRandomAccessIterator
      (also satisfiesLegacyBidirectionalIterator)
      RequiredRequiredRequiredRequiredRequired
         LegacyContiguousIterator[1]
      (also satisfiesLegacyRandomAccessIterator)
      RequiredRequiredRequiredRequiredRequiredRequired
      1. LegacyContiguousIterator category was only formally specified in C++17, but the iterators ofstd::vector,std::basic_string,std::array, andstd::valarray, as well as pointers into C arrays are often treated as a separate category in pre-C++17 code.

      Note: A type supporting the required operations in a row of the table above does not necessarily fall into the corresponding category, see the category page for the complete list of requirements.

      [edit]Definitions

      [edit]Types and writability

      An input iteratori supports the expression*i, resulting in a value of someobject typeT, called thevalue type of the iterator.

      An output iteratori has a non-empty set of types that arewritable(until C++20)indirectly_writable(since C++20) to the iterator; for each such typeT, the expression*i= o is valid whereo is a value of typeT.

      For every iterator typeXfor which equality is defined(until C++20), there is a corresponding signedinteger(until C++20)integer-like(since C++20) type called thedifference type of the iterator.

      [edit]Dereferenceability and validity

      Just as a regular pointer to anarray guarantees that there is a pointer value pointing past the last element of the array, so for any iterator type there is an iterator value that points past the last element of a corresponding sequence. Such a value is called apast-the-end value.

      Values of an iteratori for which the expression*i is defined are calleddereferenceable. Thestandard library never assumes that past-the-end values are dereferenceable.

      Iterators can also havesingular values that are not associated with any sequence. Results of most expressions are undefined for singular values; the only exceptions are

      • the assignment of a non-singular value to an iterator that holds a singular value,
      • destroying an iterator that holds a singular value, and,
      • for iterators that meet theDefaultConstructible requirements, using avalue-initialized iterator as the source of a copyor move(since C++11) operation.

      In these cases the singular value is overwritten the same way as any other value. Dereferenceable values are always non-singular.

      Aninvalid iterator is an iterator that may be singular.

      [edit]Ranges

      Most of the standard library’s algorithmic templates that operate on data structures have interfaces that use ranges.

      An iteratorj is calledreachable from an iteratori if and only if there is a finite sequence of applications of the expression++i that makesi== j. Ifj is reachable fromi, they refer to elements of the same sequence.

      Arange is a pair of iterators that designate the beginning and end of the computation. A range[ii) is an empty range; in general, a range[ij) refers to the elements in the data structure starting with the element pointed to byi and up to but not including the element pointed to byj.

      Range[ij) isvalid if and only ifj is reachable fromi.

      (until C++20)

      Arange can be denoted by either

      • [is), with an iteratori and asentinels that designate the beginning and end of the computation (i ands can have different types), or
      • i + [0n), with an iteratori and a countn that designate the beginning and the number of elements to which the computation is to be applied.
      Iterator-sentinel range

      An iterator and a sentinel denoting a range are comparable.[is) is empty ifi== s; otherwise,[is) refers to the elements in the data structure starting with the element pointed to byi and up to but not including the element, if any, pointed to by the first iteratorj such thatj== s.

      A sentinels is calledreachable from an iteratori if and only if there is a finite sequence of applications of the expression++i that makesi== s.

      Ifs is reachable fromi,[is) denotes avalid range.

      Counted range

      Acounted rangei + [0n) is empty ifn==0; otherwise,i + [0n) refers to then elements in the data structure starting with the element pointed to byi and up to but not including the element, if any, pointed to by the result ofn applications of++i.

      A counted rangei + [0n) isvalid if and only if

      • n==0; or
      • all of the following conditions are satisfied:
        • n is positive,
        • i is dereferenceable, and
        • ++i + [0--n) is valid.
      (since C++20)

      The result of the application of functions in the standard library to invalid ranges is undefined.

      [edit]Iterator concepts(since C++20)

      A new system of iterators based onconcepts that are different from C++17 iterators. While the basic taxonomy remains similar, the requirements for individual iterator categories are somewhat different.

      Defined in namespacestd
      specifies that a type is indirectly readable by applying operator*
      (concept)[edit]
      specifies that a value can be written to an iterator's referenced object
      (concept)[edit]
      specifies that asemiregular type can be incremented with pre- and post-increment operators
      (concept)[edit]
      specifies that the increment operation on aweakly_incrementable type isequality-preserving and that the type isequality_comparable
      (concept)[edit]
      specifies that a type behaves as a (signed) integer type
      (exposition-only concept*)[edit]
      specifies that objects of a type can be incremented and dereferenced
      (concept)[edit]
      specifies a type is a sentinel for aninput_or_output_iterator type
      (concept)[edit]
      specifies that the- operator can be applied to an iterator and a sentinel to calculate their difference in constant time
      (concept)[edit]
      specifies that a type is an input iterator, that is, its referenced values can be read and it can be both pre- and post-incremented
      (concept)[edit]
      specifies that a type is an output iterator for a given value type, that is, values of that type can be written to it and it can be both pre- and post-incremented
      (concept)[edit]
      specifies that aninput_iterator is a forward iterator, supporting equality comparison and multi-pass
      (concept)[edit]
      specifies that aforward_iterator is a bidirectional iterator, supporting movement backwards
      (concept)[edit]
      specifies that abidirectional_iterator is a random-access iterator, supporting advancement in constant time and subscripting
      (concept)[edit]
      specifies that arandom_access_iterator is a contiguous iterator, referring to elements that are contiguous in memory
      (concept)[edit]

      [edit]Iterator associated types(since C++20)

      Defined in namespacestd
      computes the difference type of aweakly_incrementable type
      (class template)[edit]
      computes the value type of anindirectly_readable type
      (class template)[edit]
      computes the associated types of an iterator
      (alias template)[edit]

      [edit]Iterator primitives

      provides uniform interface to the properties of an iterator
      (class template)[edit]
      empty class types used to indicate iterator categories
      (class)[edit]
      (deprecated in C++17)
      base class to ease the definition of required types for simple iterators
      (class template)[edit]

      [edit]Iterator customization points(since C++20)

      Defined in namespacestd::ranges
      (C++20)
      casts the result of dereferencing an object to its associated rvalue reference type
      (customization point object)[edit]
      (C++20)
      swaps the values referenced by two dereferenceable objects
      (customization point object)[edit]

      [edit]Algorithm concepts and utilities(since C++20)

      A set of concepts and related utility templates designed to ease constraining common algorithm operations.

      Defined in header<iterator>
      Defined in namespacestd
      Indirect callable concepts
      specifies that a callable type can be invoked with the result of dereferencing anindirectly_readable type
      (concept)[edit]
      specifies that a callable type, when invoked with the result of dereferencing anindirectly_readable type, satisfiespredicate
      (concept)[edit]
      specifies that a callable type, when invoked with the result of dereferencing twoindirectly_readable types, satisfiespredicate
      (concept)[edit]
      specifies that a callable type, when invoked with the result of dereferencing twoindirectly_readable types, satisfiesequivalence_relation
      (concept)[edit]
      specifies that a callable type, when invoked with the result of dereferencing twoindirectly_readable types, satisfiesstrict_weak_order
      (concept)[edit]
      Common algorithm requirements
      specifies that values may be moved from anindirectly_readable type to anindirectly_writable type
      (concept)[edit]
      specifies that values may be moved from anindirectly_readable type to anindirectly_writable type and that the move may be performed via an intermediate object
      (concept)[edit]
      specifies that values may be copied from anindirectly_readable type to anindirectly_writable type
      (concept)[edit]
      specifies that values may be copied from anindirectly_readable type to anindirectly_writable type and that the copy may be performed via an intermediate object
      (concept)[edit]
      specifies that the values referenced by twoindirectly_readable types can be swapped
      (concept)[edit]
      specifies that the values referenced by twoindirectly_readable types can be compared
      (concept)[edit]
      (C++20)
      specifies the common requirements of algorithms that reorder elements in place
      (concept)[edit]
      (C++20)
      specifies the requirements of algorithms that merge sorted sequences into an output sequence by copying elements
      (concept)[edit]
      (C++20)
      specifies the common requirements of algorithms that permute sequences into ordered sequences
      (concept)[edit]
      Utilities
      computes the result of invoking a callable object on the result of dereferencing some set ofindirectly_readable types
      (alias template)[edit]
      (C++20)
      helper template for specifying the constraints on algorithms that accept projections
      (alias template)[edit]
      computes the value type of anindirectly_readable type by projection
      (alias template)[edit]

      [edit]Iterator adaptors

      iterator adaptor for reverse-order traversal
      (class template)[edit]
      creates astd::reverse_iterator of type inferred from the argument
      (function template)[edit]
      iterator adaptor for insertion at the end of a container
      (class template)[edit]
      creates astd::back_insert_iterator of type inferred from the argument
      (function template)[edit]
      iterator adaptor for insertion at the front of a container
      (class template)[edit]
      creates astd::front_insert_iterator of type inferred from the argument
      (function template)[edit]
      iterator adaptor for insertion into a container
      (class template)[edit]
      creates astd::insert_iterator of type inferred from the argument
      (function template)[edit]
      iterator adaptor that converts an iterator into a constant iterator
      (class template)[edit]
      computes a constant iterator type for a given type
      (alias template)[edit]
      computes a sentinel type to be used with constant iterators
      (alias template)[edit]
      creates astd::const_iterator of type inferred from the argument
      (function template)[edit]
      creates astd::const_sentinel of type inferred from the argument
      (function template)[edit]
      iterator adaptor which dereferences to an rvalue
      (class template)[edit]
      sentinel adaptor forstd::move_iterator
      (class template)[edit]
      creates astd::move_iterator of type inferred from the argument
      (function template)[edit]
      adapts an iterator type and its sentinel into a common iterator type
      (class template)[edit]
      default sentinel for use with iterators that know the bound of their range
      (class)[edit]
      iterator adaptor that tracks the distance to the end of the range
      (class template)[edit]
      sentinel that always compares unequal to anyweakly_incrementable type
      (class)[edit]

      [edit]Stream iterators

      input iterator that reads fromstd::basic_istream
      (class template)[edit]
      output iterator that writes tostd::basic_ostream
      (class template)[edit]
      input iterator that reads fromstd::basic_streambuf
      (class template)[edit]
      output iterator that writes tostd::basic_streambuf
      (class template)[edit]

      [edit]Iterator operations

      Defined in header<iterator>
      advances an iterator by given distance
      (function template)[edit]
      returns the distance between two iterators
      (function template)[edit]
      (C++11)
      increment an iterator
      (function template)[edit]
      (C++11)
      decrement an iterator
      (function template)[edit]
      advances an iterator by given distance or to a given bound
      (algorithm function object)[edit]
      returns the distance between an iterator and a sentinel, or between the beginning and end of a range
      (algorithm function object)[edit]
      increment an iterator by a given distance or to a bound
      (algorithm function object)[edit]
      decrement an iterator by a given distance or to a bound
      (algorithm function object)[edit]

      [edit]Range access(since C++11)

      These non-member function templates provide a generic interface for containers, plain arrays, andstd::initializer_list.

      Defined in header<array>
      Defined in header<deque>
      Defined in header<flat_map>
      Defined in header<flat_set>
      Defined in header<forward_list>
      Defined in header<inplace_vector>
      Defined in header<iterator>
      Defined in header<list>
      Defined in header<map>
      Defined in header<regex>
      Defined in header<set>
      Defined in header<span>
      Defined in header<string>
      Defined in header<string_view>
      Defined in header<unordered_map>
      Defined in header<unordered_set>
      Defined in header<vector>
      Defined in namespacestd
      (C++11)(C++14)
      returns an iterator to the beginning of a container or array
      (function template)[edit]
      (C++11)(C++14)
      returns an iterator to the end of a container or array
      (function template)[edit]
      returns a reverse iterator to the beginning of a container or array
      (function template)[edit]
      (C++14)
      returns a reverse end iterator for a container or array
      (function template)[edit]
      (C++17)(C++20)
      returns the size of a container or array
      (function template)[edit]
      (C++17)
      checks whether the container is empty
      (function template)[edit]
      (C++17)
      obtains the pointer to the underlying array
      (function template)[edit]

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 1181C++98array types could not be value typesthey can
      LWG 208C++98past-the-end iterators were always non-singularthey can be singular
      LWG 278C++98the validity of an iterator was not defineddefined to be always non-singular
      LWG 324C++98output iterators had value typesoutput iterators have writable types instead
      LWG 407C++98singular iterators could not be destroyedallowed
      LWG 408
      (N3066)
      C++98singular iterators could not be copiedallowed if they are value-initialized
      LWG 1185
      (N3066)
      C++98LegacyForwardIterator,LegacyBidirectionalIterator
      andLegacyRandomAccessIterator
      always satisfyLegacyOutputIterator
      they might not satisfyLegacyOutputIterator
      LWG 1210
      (N3066)
      C++98the definition of iterator singularity and
      reachability depended on containers
      depend on sequences instead
      LWG 3009C++17<string_view> did not provide the
      range access function templates
      provides these templates
      LWG 3987C++23<flat_map> and<flat_set> did not
      provide the range access function templates
      provide these templates
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator&oldid=179898"

      [8]ページ先頭

      ©2009-2025 Movatter.jp