Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/input or output iterator"

From cppreference.com
< cpp‎ | iterator
m (Example: ~)
m (in-house formatting.)
Line 1: Line 1:
 
{{cpp/title|input_or_output_iterator}}
 
{{cpp/title|input_or_output_iterator}}
 
{{cpp/iterator/navbar}}
 
{{cpp/iterator/navbar}}
{{dcl begin}}
+
{{ddcl|header=iterator|since=c++20|1=
{{dcl header|iterator}}
+
{{dcl|since=c++20|1=
+
 
template< class I >
 
template< class I >
 
     concept input_or_output_iterator =
 
     concept input_or_output_iterator =
Line 11: Line 9:
 
         std::weakly_incrementable<I>;
 
         std::weakly_incrementable<I>;
 
}}
 
}}
{{dcl end}}
 
  
 
The {{tt|input_or_output_iterator}} concept forms the basis of the iterator concept taxonomy; every iterator type satisfies the {{tt|input_or_output_iterator}} requirements.
 
The {{tt|input_or_output_iterator}} concept forms the basis of the iterator concept taxonomy; every iterator type satisfies the {{tt|input_or_output_iterator}} requirements.
Line 31: Line 28:
 
===Example===
 
===Example===
 
A minimum iterator.
 
A minimum iterator.
 
+
{{source|1=
{{example
+
|code=
+
 
#include <cstddef>
 
#include <cstddef>
 
#include <iterator>
 
#include <iterator>
  
struct SimpleIterator {
+
struct SimpleIterator
 +
{
 
     using difference_type = std::ptrdiff_t;
 
     using difference_type = std::ptrdiff_t;
  
Line 47: Line 43:
  
 
static_assert(std::input_or_output_iterator<SimpleIterator>);
 
static_assert(std::input_or_output_iterator<SimpleIterator>);
 
|output=
 
 
}}
 
}}
  
 
{{langlinks|es|ja|zh}}
 
{{langlinks|es|ja|zh}}

Revision as of 13:26, 13 May 2024

 
 
Iterator library
Iterator concepts
input_or_output_iterator
(C++20)

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)
 
Defined in header <iterator>
template< class I >

    concept input_or_output_iterator =
        requires(I i) {
            { *i } -> /*can-reference*/;
        } &&

        std::weakly_incrementable<I>;
(since C++20)

The input_or_output_iterator concept forms the basis of the iterator concept taxonomy; every iterator type satisfies the input_or_output_iterator requirements.

The exposition-only concept /*can-reference*/ is satisfied if and only if the type is referenceable (in particular, not void).

Notes

input_or_output_iterator itself only specifies operations for dereferencing and incrementing an iterator. Most algorithms will require additional operations, for example:

Unlike the LegacyIterator requirements, the input_or_output_iterator concept does not require copyability.

Example

A minimum iterator.

#include <cstddef>
#include <iterator>
 
struct SimpleIterator
{
    using difference_type = std::ptrdiff_t;
 
    int operator*();
 
    SimpleIterator& operator++();
    void operator++(int) { ++*this; }
};
 
static_assert(std::input_or_output_iterator<SimpleIterator>);