operator-(std::common_iterator)
來自 cppreference.com
< cpp | 迭代器 | common_iterator
template< std::sized_sentinel_for<I> I2, std::sized_sentinel_for<I> S2 > requires std::sized_sentinel_for<S, I2> |
(C++20 起) | |
計算兩個迭代器介面卡之間的距離。兩個哨兵被認為是相等的。
設 var
表示 std::common_iterator 中底層的 std::variant 成員物件,如果 x 或 y 無效,即 x.var.valueless_by_exception() || y.var.valueless_by_exception() 為 true,則行為未定義。
此函式模板對普通的非限定或限定查詢不可見,只能透過實參依賴查詢在 std::common_iterator<I> 是實參的關聯類時被找到。
目錄 |
[編輯] 引數
x, y | - | 要計算差異的迭代器介面卡 |
[編輯] 返回值
- 0 如果 x.var 持有一個
S
物件且 y.var 持有一個S2
物件,即它們都持有一個哨兵。 - 否則,為 alt_x - alt_y,其中
alt_x
和alt_y
分別是 x.var 和 y.var 所持有的備選項(要麼是兩個迭代器,要麼是一個迭代器和一個哨兵)。
[編輯] 示例
執行此程式碼
#include <algorithm> #include <iostream> #include <iterator> int main() { int a[]{0, 1, 2, 3, 4, 5}; using CI = std::common_iterator< std::counted_iterator<int*>, std::default_sentinel_t >; CI i1{std::counted_iterator{a + 1, 2}}; CI i2{std::counted_iterator{a, 3}}; CI s1{std::default_sentinel}; CI s2{std::default_sentinel}; std::cout << (s2 - s1) << ' ' << (i2 - i1) << ' ' << (i1 - s1) << '\n'; }
輸出
0 -1 -2
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 3574 | C++20 | variant 完全為 constexpr (P2231R1) 但 common_iterator 不是 |
也設為 constexpr |
[編輯] 參閱
前進 common_iterator (公共成員函式) |