“cpp/iterator/counted iterator/operator-”的版本间的差异
来自cppreference.com
< cpp | iterator | counted iterator
小 (→返回值) |
小 |
||
第1行: | 第1行: | ||
− | {{title | operator-<small>(std::counted_iterator)</small>}} | + | {{title|operator-<small>(std::counted_iterator)</small>}} |
{{cpp/iterator/counted_iterator/navbar}} | {{cpp/iterator/counted_iterator/navbar}} | ||
{{ddcl|since=c++20| | {{ddcl|since=c++20| | ||
template< std::common_with<I> I2 > | template< std::common_with<I> I2 > | ||
− | + | friend constexpr std::iter_difference_t<I2> operator-( | |
− | + | const counted_iterator& x, const counted_iterator<I2>& y ); | |
}} | }} | ||
− | 计算 | + | 计算 个迭代器适配器间的距离。 |
− | 若 {{ | + | 若 {{|x}} 与 {{|y}} 不指向同一序列的元素则行为未定义。即必须存在某个 {{c|n}} 使得 {{c|1=std::next(x.base(), x.count() + n)}} 与 {{c|std::next(y.base(), y.count() + n)}} 指代同一元素。 |
{{cpp/hidden friend|std::counted_iterator<I>|tmpl=yes}} | {{cpp/hidden friend|std::counted_iterator<I>|tmpl=yes}} | ||
第15行: | 第15行: | ||
===参数=== | ===参数=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | x, y | | + | {{par|x, y|要计算差的迭代器适配器}} |
{{par end}} | {{par end}} | ||
===返回值=== | ===返回值=== | ||
− | {{c|y.count() - x.count()}} | + | {{c|y.count() - x.count()}} |
===注解=== | ===注解=== | ||
− | 由于''长度''向下计数,而非向上, {{c|1=operator | + | 由于''长度''向下计数,而非向上,{{c|1=operator}} 的 参在底层表达式中的顺序是逆转的,即 {{|y}} 为 ''lhs'' 而 {{|x}} 为 ''rhs''。 |
===示例=== | ===示例=== | ||
− | {{example|code= | + | {{example |
+ | |code= | ||
#include <initializer_list> | #include <initializer_list> | ||
#include <iterator> | #include <iterator> | ||
第33行: | 第34行: | ||
static constexpr auto v = {1, 2, 3, 4, 5, 6}; | static constexpr auto v = {1, 2, 3, 4, 5, 6}; | ||
constexpr std::counted_iterator<std::initializer_list<int>::iterator> | constexpr std::counted_iterator<std::initializer_list<int>::iterator> | ||
− | it1 {v.begin(), 5}, | + | it1{v.begin(), 5}, |
− | it2 {it1 + 3}, | + | it2{it1 + 3}, |
− | it3 {v.begin(), 2}; | + | it3{v.begin(), 2}; |
static_assert(it1 - it2 == -3); | static_assert(it1 - it2 == -3); | ||
static_assert(it2 - it1 == +3); | static_assert(it2 - it1 == +3); | ||
− | + | // static_assert(it1 - it3 == -3); // UB - 的操作数不指代同一序列的元素 | |
} | } | ||
}} | }} | ||
第45行: | 第46行: | ||
===参阅=== | ===参阅=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/iterator/adaptor/dsc operator_arith|counted_iterator}} | + | {{dsc inc|cpp/iterator/adaptor/dsc operator_arith|counted_iterator}} |
− | {{dsc inc | cpp/iterator/adaptor/dsc operator+|counted_iterator}} | + | {{dsc inc|cpp/iterator/adaptor/dsc operator+|counted_iterator}} |
− | {{dsc inc | cpp/iterator/counted_iterator/dsc operator-2}} | + | {{dsc inc|cpp/iterator/counted_iterator/dsc operator-2}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|de|en|es|fr|it|ja|pt|ru}} | {{langlinks|de|en|es|fr|it|ja|pt|ru}} |
2023年12月18日 (一) 09:24的最后版本
template< std::common_with<I> I2 > friend constexpr std::iter_difference_t<I2> operator-( |
(C++20 起) | |
计算两个迭代器适配器间的距离。
若 x 与 y 不指向同一序列的元素则行为未定义。即必须存在某个 n 使得 std::next(x.base(), x.count() + n) 与 std::next(y.base(), y.count() + n) 指代同一元素。
此函数模板对常规的无限定或有限定查找不可见,而只能在 std::counted_iterator<I> 为实参的关联类时由实参依赖查找找到。
目录 |
[编辑] 参数
x, y | - | 要计算差的迭代器适配器 |
[编辑] 返回值
y.count() - x.count()
[编辑] 注解
由于长度向下计数,而非向上,operator- 的实参在底层表达式中的顺序是逆转的,即 y 为 lhs 而 x 为 rhs。
[编辑] 示例
运行此代码
#include <initializer_list> #include <iterator> int main() { static constexpr auto v = {1, 2, 3, 4, 5, 6}; constexpr std::counted_iterator<std::initializer_list<int>::iterator> it1{v.begin(), 5}, it2{it1 + 3}, it3{v.begin(), 2}; static_assert(it1 - it2 == -3); static_assert(it2 - it1 == +3); // static_assert(it1 - it3 == -3); // UB:operator- 的操作数不指代同一序列的元素 }
[编辑] 参阅
推进或回退 counted_iterator (公开成员函数) | |
(C++20) |
令迭代器前进 (函数模板) |
计算到末尾的有符号距离 (函数模板) |