Skip to content

Commit d528729

Browse files
TylerMSFTTylerMSFT
authored andcommitted
draft
1 parent d77c253 commit d528729

File tree

4 files changed

+29
-50
lines changed

4 files changed

+29
-50
lines changed

docs/standard-library/iterator-concepts.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
description: "Learn more about iterator concepts."
3-
title: "<iterator> concepts"
3+
title: "Iterator concepts"
44
ms.date: 12/05/2022
55
f1_keywords: ["ranges/std::ranges::range", "ranges/std::ranges::bidirectional_iterator", "ranges/std::ranges::borrowed_iterator", "ranges/std::ranges::common_iterator", "ranges/std::ranges::contiguous_iterator", "ranges/std::ranges::forward_iterator", "ranges/std::ranges::input_iterator", "ranges/std::ranges::output_iterator", "ranges/std::ranges::random_access_iterator", "ranges/std::ranges::simple_view", "ranges/std::ranges::sized_iterator", "ranges/std::ranges::view", "ranges/std::ranges::viewable_iterator"]
66
helpviewer_keywords: ["std::ranges [C++], ranges::range", "std::ranges [C++], ranges::bidirectional_iterator", "std::ranges [C++], ranges::borrowed_iterator", "std::ranges [C++], ranges::common_iterator", "std::ranges [C++], ranges::contiguous_iterator", "std::ranges [C++], ranges::forward_iterator", "std::ranges [C++], ranges::input_iterator", "std::ranges [C++], ranges::output_iterator", "std::ranges [C++], ranges::random_access_iterator", "std::ranges [C++], ranges::simple_view", "std::ranges [C++], ranges::sized_iterator", "std::ranges [C++], ranges::view", "std::ranges [C++], ranges::viewable_iterator"]
77
---
8-
# `<iterator>` concepts
8+
# Iterator concepts
99

1010
Concepts are a C++20 language feature that constrain template parameters at compile time. They help prevent incorrect template instantiation, specify template argument requirements in a readable form, and provide more succinct template related compiler errors.
1111

@@ -52,17 +52,19 @@ When you pass the compiler switch `/diagnostics:caret` to Visual Studio 2022 ver
5252

5353
Iterator concepts are defined in the `std` namespace as declared in the `<iterator>` header file. They're used in the declarations of [range adaptors](range-adaptors.md), [views](view-classes.md), and so on.
5454

55-
There are six categories of iterators. They are directly related to the categories of ranges listed in [`<ranges>`](ranges.md#kinds-of-ranges). In order of increasing capability, the categories are:
55+
There are six categories of iterators. They are directly related to the categories of ranges listed in [`<ranges>`](ranges.md#kinds-of-ranges).
5656

57-
| Iterator concept | Description |
58-
|--|--|
59-
DONE | [`input_or_output_iterator`](#input_or_output_iterator)<sup>C++20</sup> | The basis of the iterator concept taxonomy. |
60-
DONE | [`output_iterator`](#output_iterator)<sup>C++20</sup> | Test for an iterator that you can write to. |
61-
DONE | [`input_iterator`](#input_iterator)<sup>C++20</sup> | Test for an iterator that you can read from at least once. |
62-
DONE | [`forward_iterator`](#forward_iterator)<sup>C++20</sup> | Test for an iterator that can read (and possibly write) multiple times. |
63-
DONE | [`bidirectional_iterator`](#bidirectional_iterator)<sup>C++20</sup> | Test for an iterator that can read and write both forwards and backwards. |
64-
DONE | [`random_access_iterator`](#random_access_iterator)<sup>C++20</sup> | Test for an iterator that can read and write by index. |
65-
DONE | [`contiguous_iterator`](#contiguous_iterator)<sup>C++20</sup> | Test for an iterator whose elements are sequential in memory and can be accessed using pointer arithmetic. |
57+
The following iterator concepts are listed in order of increasing capability. `input_or_output_iterator` is at the low end of the capability hierarchy, and `contiguous_iterator` is at the high end. Iterators higher in the hierarchy can generally be used in place of those that are lower, but not vice-versa. For example, a `random_access_iterator` iterator can be used in place of a `forward_iterator`, but not the other way around. An exception to the rule that iterators higher in the hierarchy can generally be used in place of those that are lower is `input_iterator`, which can't be used in place of `output_iterator` because it can't write.
58+
59+
| Iterator concept | Description | Direction | Read/write | Multi-pass | Example types that use this iterator |
60+
|--|--|--|--|--|--|
61+
| [`input_or_output_iterator`](#input_or_output_iterator)<sup>C++20</sup> | The basis of the iterator concept taxonomy. | Forward | Read or write | Single-pass | `std::istream_iterator`, `std::ostream_iterator` |
62+
| [`output_iterator`](#output_iterator)<sup>C++20</sup> | Test for an iterator that you can write to. | Forward | Write | Single-pass | `ostream`, `inserter` |
63+
| [`input_iterator`](#input_iterator)<sup>C++20</sup> | Test for an iterator that you can read from at least once. | Forward | Read | Single-pass | `istream`, `istreambuf_iterator` |
64+
| [`forward_iterator`](#forward_iterator)<sup>C++20</sup> | Test for an iterator that can read (and possibly write) multiple times. | Forward | Read/write | Multi-pass | `vector::iterator`, `list::iterator` |
65+
| [`bidirectional_iterator`](#bidirectional_iterator)<sup>C++20</sup> | Test for an iterator that can read and write both forwards and backwards. | Forward/Backward | Read/write | Multi-pass | `list`, `set`, `multiset`, `map`, and `multimap`. |
66+
| [`random_access_iterator`](#random_access_iterator)<sup>C++20</sup> | Test for an iterator that can read and write by index. | Read/write | Multi-pass | `vector::iterator`, `array::iterator` |
67+
| [`contiguous_iterator`](#contiguous_iterator)<sup>C++20</sup> | Test for an iterator whose elements are sequential in memory and can be accessed using pointer arithmetic. | Read/write | Multi-pass | `array`, `vector` |
6668

6769
An iterator that meets the requirements of a concept generally meets the requirements of the concepts in the rows that precede it in the previous table. For example, a `random_access_iterator` has the capability of a `bidirectional_iterator`, `forward_iterator`, `input_iterator`, and `output_iterator`. An exception is `input_iterator` which doesn't have the capability of an `output_iterator` because it can't be written to.
6870

docs/standard-library/iterator.md

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
description: "Learn more about: <iterator>"
32
title: "<iterator>"
3+
description: "Learn more about: <iterator>"
44
ms.date: 12/14/2022
55
f1_keywords: ["<iterator>"]
66
helpviewer_keywords: ["iterator header"]
@@ -9,10 +9,6 @@ helpviewer_keywords: ["iterator header"]
99

1010
Defines predefined iterators and stream iterators, iterator primitives, and supporting templates.
1111

12-
The predefined iterators include insert and reverse adaptors.
13-
14-
There are three classes of insert iterator adaptors: front, back, and general. They provide insert semantics rather than overwrite semantics that the container member function iterators provide.
15-
1612
## Requirements
1713

1814
**Header:** `<iterator>`
@@ -21,30 +17,9 @@ There are three classes of insert iterator adaptors: front, back, and general. T
2117

2218
## Remarks
2319

24-
Iterators are a generalization of pointers that allow a C++ program to work with different data structures in a uniform way. Instead of operating on specific data types, algorithms operate on a range specified by a type of iterator. Any data structure that satisfies the requirements of the iterator can be operated upon by the algorithm. In C++20, there are 6 categories of iterators:
25-
26-
JTW fix this table up
20+
Iterators are a generalization of pointers that allow a C++ program to work with different data structures in a uniform way. Instead of operating on specific data types, algorithms operate on a range of values as specified by a kind of iterator. Algorithms can operate on any data structure that satisfies the requirements of the iterator.
2721

28-
| Kind | Direction | Read/Write| Example types|
29-
|---|---|---|---|
30-
| `bidirectional_iterator` | Forward and backward | Read/Write | `list`, `set`, `multiset`, `map`, and `multimap`. |
31-
| `contiguous_iterator` | Forward and backward | Read/Write | JTW |
32-
| `forward_iterator` | Forward | Read/Write | `vector` |
33-
| `input_iterator` | Forward | Read | `istream`|
34-
| `output_iterator` | Forward | Write | `ostream`, `inserter` |
35-
| `random_access_iterator` | Any order | Read/Write | `vector`, `deque`, `string`, and `array`. |
36-
37-
Until In C++17, there are five types or categories of iterators:
38-
39-
| Kind | Direction | Read/Write| Multipass | Some example types|
40-
|---|---|---|---|
41-
| Output | Forward | Write | No | `ostream`, `inserter` |
42-
| Input | Forward | Read | No | `istream`|
43-
| Forward | Forward | Read/Write | Yes | `vector`, `unordered_set`, `unordered_multiset`, `unordered_map`, and `unordered_multimap`. |
44-
| Bidirectional | Forward and backward | Read/Write | Yes | `list`, `set`, `multiset`, `map`, and `multimap`. |
45-
| Random access | Any order | Read/Write | Yes | `vector`, `deque`, `string`, and `array`. |
46-
47-
Iterators are arranged in a hierarchy of capability. In the table above, output iterators are at the low end of the capability hierarchy, and random-access iterators are at the high end. Iterators higher in the hierarchy can generally be used in place of those that are lower, but not vice-versa. For example, a random-access iterator can be used in place of a forward iterator, but not the other way around.
22+
In C++20, there are 6 categories of iterators. Iterators are arranged in a hierarchy of capability. Their capabilities are specified by C++20 concepts. For a description of the various iterators and their capabilities, see [Iterator concepts](iterator-concepts.md)
4823

4924
Visual Studio has added extensions to C++ Standard Library iterators to support debugging for checked and unchecked iterators. For more information, see [Safe Libraries: C++ Standard Library](../standard-library/safe-libraries-cpp-standard-library.md).
5025

@@ -114,17 +89,19 @@ Visual Studio has added extensions to C++ Standard Library iterators to support
11489

11590
## Concepts
11691

92+
The following concepts are defined in the `std` namespace. They apply to iterators, and are related to the iterator categories for ranges described in [`<ranges>` concepts](range-concepts.md).
93+
11794
| Iterator concept | Description |
11895
|--|--|
119-
| [`bidirectional_iterator`](iterator-concepts.md#bidirectional_iterator) | Specifies an iterator that can read and write both forwards and backwards. |
120-
| [`contiguous_iterator`](iterator-concepts.md#contiguous_iterator) | Specifies an iterator whose elements are sequential in memory and can be accessed using pointer arithmetic. |
121-
| [`forward_iterator`](iterator-concepts.md#forward_iterator) | Specifies an iterator that can read (and possibly write) multiple times. |
122-
| [`input_iterator`](iterator-concepts.md#input_iterator) | Specifies an iterator that you can read from at least once. |
123-
| [`input_or_output_iterator`](iterator-concepts.md#input_or_output_iterator) | The basis of the iterator concept taxonomy. |
96+
| [`bidirectional_iterator`](iterator-concepts.md#bidirectional_iterator)<sup>C++20</sup> | Specifies an iterator that can read and write both forwards and backwards. |
97+
| [`contiguous_iterator`](iterator-concepts.md#contiguous_iterator)<sup>C++20</sup> | Specifies an iterator whose elements are sequential in memory and can be accessed using pointer arithmetic. |
98+
| [`forward_iterator`](iterator-concepts.md#forward_iterator)<sup>C++20</sup> | Specifies an iterator that can read (and possibly write) multiple times. |
99+
| [`input_iterator`](iterator-concepts.md#input_iterator)<sup>C++20</sup> | Specifies an iterator that you can read from at least once. |
100+
| [`input_or_output_iterator`](iterator-concepts.md#input_or_output_iterator)<sup>C++20</sup> | The basis of the iterator concept taxonomy. |
124101
| [`output_iterator`](iterator-concepts.md#output_iterator) | Specifies an iterator that you can write to. |
125-
| [`random_access_iterator`](iterator-concepts.md#random_access_iterator) | Specifies an iterator that can read and write by index. |
126-
| [`sentinel_for`](iterator-concepts.md#sentinel_for)<sup>C++20</sup> | JTW |
127-
| [`sized_sentinel_for`](iterator-concepts.md#sized_sentinel_for)<sup>C++20</sup> | JTW |
102+
| [`random_access_iterator`](iterator-concepts.md#random_access_iterator)<sup>C++20</sup> | Specifies an iterator that can read and write by index. |
103+
| [`sentinel_for`](iterator-concepts.md#sentinel_for)<sup>C++20</sup> | Test that a type is a sentinel for an iterator type. |
104+
| [`sized_sentinel_for`](iterator-concepts.md#sized_sentinel_for)<sup>C++20</sup> | Specifies that an iterator and its sentinel can be subtracted (using `-`) to find their difference in constant time. |
128105

129106
## See also
130107

docs/standard-library/ranges.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ The range algorithms are almost identical to the corresponding iterator-pair alg
116116

117117
### Kinds of ranges
118118

119-
How you can iterate over the elements of a range depends on its underlying iterator type. The iterator type a range requires is specified as a C++20 concept.
119+
How you can iterate over the elements of a range depends on its underlying iterator type. Ranges use C++ concepts to specify the iterator they support.
120120

121121
In C++20, to say that concept *X* refines concept *Y* means that everything that satisfies concept *Y* also satisfies concept *X*. For example: *car*, *bus*, and *truck* all refine *vehicle*.
122122

docs/standard-library/view-classes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Each view class topic has a **Characteristics** section after the syntax section
146146

147147
### Ranges iterator hierarchy
148148

149-
In the **Characteristics** section of each view class topic, the iterator category in **Underlying range** and **View iterator category** refers to the kind of iterators that ranges and views support. Ranges iterators are categorized into six levels of capability, which are identified by C++20 concepts. That hierarchy, in increasing order of capability, is:
149+
In the **Characteristics** section of each view class topic, the iterator category in **Underlying range** and **View iterator category** refers to the kind of iterator that the range/view supports. Ranges iterators are categorized into six levels of capability, which are identified by C++20 concepts. That hierarchy, in increasing order of capability, is:
150150

151151
| Range iterator concept | Description |
152152
|--|--|

0 commit comments

Comments
 (0)