Movatterモバイル変換


[0]ホーム

URL:


24 Iterators library[iterators]

24.3 Iterator requirements[iterator.requirements]

24.3.5 C++17 iterator requirements[iterator.cpp17]


24.3.5.1 General[iterator.cpp17.general]

24.3.5.2Cpp17Iterator[iterator.iterators]

24.3.5.3 Input iterators[input.iterators]

24.3.5.4 Output iterators[output.iterators]

24.3.5.5 Forward iterators[forward.iterators]

24.3.5.6 Bidirectional iterators[bidirectional.iterators]

24.3.5.7 Random access iterators[random.access.iterators]


24.3.5.1 General[iterator.cpp17.general]

In the following sections,aandbdenote values of typeX orconst X,difference_type andreference refer to thetypesiterator_traits<X>​::​difference_type anditerator_traits<X>​::​reference, respectively,ndenotes a value ofdifference_type,u,tmp,andmdenote identifiers,rdenotes a value ofX&,tdenotes a value of value typeT,odenotes a value of some type that is writable to the output iterator.
[Note 1: 
For an iterator typeX there must be an instantiationofiterator_traits<X> ([iterator.traits]).
— end note]

24.3.5.2Cpp17Iterator[iterator.iterators]

TheCpp17Iterator requirements form the basis of the iteratortaxonomy; every iterator meets theCpp17Iterator requirements.
Thisset of requirements specifies operations for dereferencing and incrementingan iterator.
Most algorithms will require additional operations toread ([input.iterators]) or write ([output.iterators]) values, orto provide a richer set of iterator movements ([forward.iterators],[bidirectional.iterators],[random.access.iterators]).
A typeX meets theCpp17Iterator requirements if
Table78Cpp17Iterator requirements [tab:iterator]
Expression
Return type
Operational
Assertion/note
semantics
pre-/post-condition
*r
unspecified
Preconditions:r is dereferenceable.
++r
X&

24.3.5.3 Input iterators[input.iterators]

A class or pointer typeXmeets the requirements of an input iterator for the value typeTifX meets theCpp17Iterator ([iterator.iterators]) andCpp17EqualityComparable (Table28) requirements andthe expressions in Table79 are valid and havethe indicated semantics.
In Table79, the termthe domain of==is used in the ordinary mathematical sense to denotethe set of values over which== is (required to be) defined.
This set can change over time.
Each algorithm places additional requirements on the domain of== for the iterator values it uses.
These requirements can be inferred from the uses that algorithmmakes of== and!=.
[Example 1: 
The callfind(a,b,x)is defined only if the value ofahas the propertypdefined as follows:b has propertypand a valueihas propertypif(*i==x)or if(*i!=xand++ihas propertyp).
— end example]
Table79Cpp17InputIterator requirements (in addition toCpp17Iterator) [tab:inputiterator]
Expression
Return type
Operational
Assertion/note
semantics
pre-/post-condition
a!= b
decltype(a!= b) modelsboolean-testable
!(a== b)
Preconditions: (a, b) is in the domain of==.
*a
reference, convertible toT
Preconditions:a is dereferenceable.

The expression
(void)*a,*a is equivalent to*a.

Ifa== b and (a, b) is in the domain of== then*a is equivalent to*b.
a->m
(*a).m
Preconditions:a is dereferenceable.
++r
X&
Preconditions:r is dereferenceable.

Postconditions:r is dereferenceable orr is past-the-end;
any copies of the previous value ofr are no longer required to be dereferenceable nor to be in the domain of==.
(void)r++
equivalent to(void)++r
*r++
convertible toT
{ T tmp=*r;
++r;
return tmp;}
Recommended practice: The implementation of an algorithm on input iteratorsshould never attempt to pass through the same iterator twice;such an algorithm should be a single pass algorithm.
[Note 1: 
For input iterators,a== b does not imply++a==++b.
(Equality does not guarantee the substitution property or referential transparency.)
Value typeT is not required to be aCpp17CopyAssignable type (Table34).
Such an algorithm can be used with istreams as the source of the input data through theistream_iteratorclass template.
— end note]

24.3.5.4 Output iterators[output.iterators]

A class or pointer typeXmeets the requirements of an output iteratorifX meets theCpp17Iterator requirements ([iterator.iterators])and the expressions in Table80are valid and have the indicated semantics.
Table80Cpp17OutputIterator requirements (in addition toCpp17Iterator) [tab:outputiterator]
Expression
Return type
Operational
Assertion/note
semantics
pre-/post-condition
*r= o
result is not used
Remarks: After this operationr is not required to be dereferenceable.

Postconditions:r is incrementable.
++r
X&
addressof(r)== addressof(++r).

Remarks: After this operationr is not required to be dereferenceable.

Postconditions:r is incrementable.
r++
convertible toconst X&
{ X tmp= r;
++r;
return tmp;}
Remarks: After this operationr is not required to be dereferenceable.

Postconditions:r is incrementable.
*r++= o
result is not used
Remarks: After this operationr is not required to be dereferenceable.

Postconditions:r is incrementable.
Recommended practice: The implementation of an algorithm on output iteratorsshould never attempt to pass through the same iterator twice;such an algorithm should be a single-pass algorithm.
[Note 1: 
The only valid use of anoperator*is on the left side of the assignment statement.
Assignment through the same value of the iterator happens only once.
Equality and inequality are not necessarily defined.
— end note]

24.3.5.5 Forward iterators[forward.iterators]

A class or pointer typeXmeets theCpp17ForwardIterator requirements if
  • X meets theCpp17InputIterator requirements ([input.iterators]),
  • X meets theCpp17DefaultConstructiblerequirements ([utility.arg.requirements]),
  • ifX is a mutable iterator,reference is a reference toT;ifX is a constant iterator,reference is a reference toconst T,
  • the expressions in Table81are valid and have the indicated semantics, and
  • objects of typeX offer the multi-pass guarantee, described below.
The domain of== for forward iterators is that of iterators over the sameunderlying sequence.
However, value-initialized iterators may be compared andshall compare equal to other value-initialized iterators of the same type.
[Note 1: 
Value-initialized iterators behave as if they refer past the end ofthe same empty sequence.
— end note]
Two dereferenceable iteratorsa andb of typeX offer themulti-pass guarantee if
  • a== b implies++a==++b and
  • X is a pointer type or the expression(void)++X(a),*a is equivalent to the expression*a.
[Note 2: 
The requirement thata== bimplies++a==++b(which is not true for input and output iterators)and the removal of the restrictions on the number of the assignments througha mutable iterator(which applies to output iterators)allows the use of multi-pass one-directional algorithms with forward iterators.
— end note]
Table81Cpp17ForwardIterator requirements (in addition toCpp17InputIterator) [tab:forwarditerator]
Expression
Return type
Operational
Assertion/note
semantics
pre-/post-condition
r++
convertible toconst X&
{ X tmp= r;
++r;
return tmp;}
*r++
reference
Ifa andb are equal, then eithera andbare both dereferenceableor else neither is dereferenceable.
Ifa andb are both dereferenceable, thena== bif and only if*a and*b are bound to the same object.

24.3.5.6 Bidirectional iterators[bidirectional.iterators]

A class or pointer typeXmeets the requirements of a bidirectional iterator if,in addition to meeting theCpp17ForwardIterator requirements,the following expressions are valid as shown in Table82.
Table82Cpp17BidirectionalIterator requirements (in addition toCpp17ForwardIterator) [tab:bidirectionaliterator]
Expression
Return type
Operational
Assertion/note
semantics
pre-/post-condition
--r
X&
Preconditions: there existss such thatr==++s.

Postconditions:r is dereferenceable.

--(++r)== r.

--r==--s impliesr== s.

addressof(r)== addressof(--r).
r--
convertible toconst X&
{ X tmp= r;
--r;
return tmp;}
*r--
reference
[Note 1: 
Bidirectional iterators allow algorithms to move iterators backward as well as forward.
— end note]

24.3.5.7 Random access iterators[random.access.iterators]

A class or pointer typeXmeets the requirements of a random access iterator if,in addition to meeting theCpp17BidirectionalIterator requirements,the following expressions are valid as shown in Table83.
Table83Cpp17RandomAccessIterator requirements (in addition toCpp17BidirectionalIterator) [tab:randomaccessiterator]
Expression
Return type
Operational
Assertion/note
semantics
pre-/post-condition
r+= n
X&
{ difference_type m= n;
if(m>=0)
while(m--)
++r;
else
while(m++)
--r;
return r;}
a+ n
n+ a
X
{ X tmp= a;
return tmp+= n;}
a+ n== n+ a.
r-= n
X&
return r+=-n;
Preconditions: the absolute value ofn is in the range of representable values ofdifference_type.
a- n
X
{ X tmp= a;
return tmp-= n;}
b- a
difference_type
return n;
Preconditions: there exists a valuen of typedifference_type such thata+ n== b.

b== a+(b- a).
a[n]
convertible toreference
*(a+ n)
a< b
decltype(a< b) modelsboolean-testable
Effects: Equivalent to:return b- a>0;
< is a total ordering relation
a> b
decltype(a> b) modelsboolean-testable
b< a
> is a total ordering relation opposite to<.
a>= b
decltype(a>= b) modelsboolean-testable
!(a< b)
a<= b
decltype(a<= b) modelsboolean-testable
!(a> b)

[8]ページ先頭

©2009-2026 Movatter.jp