operator-(std::common_iterator)
来自cppreference.com
| |
(C++20 起) | |
计算底层 std::variant 成员对象 var 所保有的迭代器与/或哨位间的距离。认为两个哨位相等。
令 Ix 为 x.var .index(),Iy 为 y.var .index():
- 在
Ix == 1 && Iy == 1(即x与y均保有哨位)时,距离是0。 - 否则距离是
std::get<Ix>(x.var) - std::get<Iy>(y.var)。
|
如果 |
(C++26 前) |
|
如果 |
(C++26 起) |
此函数模板对常规的无限定或有限定查找不可见,而只能在 std::common_iterator<I> 为实参的关联类时由实参依赖查找找到。
参数
| x, y | - | 要计算差的迭代器适配器 |
返回值
底层迭代器与/或哨位间的距离,如上所述。
示例
运行此代码
#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 (公开成员函数) |