C++ 命名需求: LegacyRandomAccessIterator
一個 LegacyRandomAccessIterator 是一個 LegacyBidirectionalIterator,它可以在常數時間內移動以指向任何元素。
如果一個 LegacyRandomAccessIterator it 源自一個 Container,那麼 it 的 value_type
與容器的相同,因此解引用 (*it) 得到容器的 value_type
。
指向陣列元素的指標滿足 LegacyRandomAccessIterator 的所有要求。
目錄 |
[編輯] 要求
如果型別 It
滿足 LegacyRandomAccessIterator:
- 型別
It
滿足 LegacyBidirectionalIterator
並且,給定
-
value_type
,由 std::iterator_traits<It>::value_type 表示的型別 -
difference_type
,由 std::iterator_traits<It>::difference_type 表示的型別 -
reference
,由 std::iterator_traits<It>::reference 表示的型別 - i, a, b,型別為
It
或 const It 的物件 - r,型別為
It
的左值 - n,型別為
difference_type
的整數
以下表達式必須有效並具有其指定的效果
表示式 | 返回型別 | 操作語義 | 注意 | ||||
---|---|---|---|---|---|---|---|
r += n | It&
|
difference_type m = n; if (m >= 0) while (m--) ++r; |
| ||||
a + n
n + a |
It
|
It temp = a; return temp += n; |
| ||||
r -= n | It& |
return r += -n; | n 的絕對值必須在 difference_type 可表示值的範圍內。 | ||||
i - n | It |
It temp = i; return temp -= n; |
|||||
b - a | difference_type |
return n; (參見前置條件) |
前置條件
後置條件
| ||||
i[n] | 可轉換為 reference |
*(i + n) | |||||
a < b |
|
等價於 return b - a > 0; | 前置條件
嚴格全序關係
| ||||
a > b |
|
b < a | 與 a < b 相反的全序關係 | ||||
a >= b |
|
!(a < b) | |||||
a <= b |
|
!(a > b) |
上述規則暗示 LegacyRandomAccessIterator 也實現了 LessThanComparable。
一個 _可變_ LegacyRandomAccessIterator 是一個 LegacyRandomAccessIterator,它額外滿足 LegacyOutputIterator 的要求。
概念對於 std::iterator_traits 的定義,定義了以下僅用於說明的概念。
其中,僅用於說明的概念 |
(C++20 起) |
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 299 (N3066) |
C++98 | 要求 a[n] 的返回型別 可轉換為 const value_type& |
要求返回型別 可轉換為 reference |
LWG 448 | C++98 | 要求 a[n] 的返回型別 可轉換為 value_type |
要求返回型別 可轉換為 const value_type&[1] |
LWG 1079 | C++98 | b - a 使用 a < b 定義, 導致迴圈定義 |
從定義中移除 a < b |
LWG 2114 (P2167R3) |
C++98 | 可轉換為 bool 對於反映實現的期望來說太弱了 | 要求已加強 |
- ↑ LWG issue 299 在此決議後重新開放。
[編輯] 另見
(C++20) |
指定一個 bidirectional_iterator 是一個隨機訪問迭代器,支援常數時間步進和下標操作(概念) |
迭代器庫 | 為迭代器、迭代器特性、介面卡和實用函式提供定義 |