std::counted_iterator<I>::operator++,+,+=,--,-,-=
来自 cppreference.cn
< cpp | iterator | counted iterator
constexpr counted_iterator& operator++(); |
(1) | (自 C++20 起) |
constexpr decltype(auto) operator++( int ); |
(2) | (自 C++20 起) |
constexpr counted_iterator operator++( int ) requires std::forward_iterator<I>; |
(3) | (自 C++20 起) |
constexpr counted_iterator& operator--() requires std::bidirectional_iterator<I>; |
(4) | (自 C++20 起) |
constexpr counted_iterator operator--( int ) requires std::bidirectional_iterator<I>; |
(5) | (自 C++20 起) |
constexpr counted_iterator operator+( std::iter_difference_t<I> n ) const requires std::random_access_iterator<I>; |
(6) | (自 C++20 起) |
constexpr counted_iterator& operator+=( std::iter_difference_t<I> n ) requires std::random_access_iterator<I>; |
(7) | (自 C++20 起) |
constexpr counted_iterator operator-( std::iter_difference_t<I> n ) const requires std::random_access_iterator<I>; |
(8) | (自 C++20 起) |
constexpr counted_iterator& operator-=( std::iter_difference_t<I> n ) requires std::random_access_iterator<I>; |
(9) | (自 C++20 起) |
递增或递减底层迭代器 current
以及到末尾的距离 length
。
如果 length
会被设置为负值,则这些函数的行为是未定义的。
1) 前置递增一。等效于 ++current; --length; return *this;。
2) 后置递增一。等效于 --length; try { return current++; } catch(...) { ++length; throw; }。
3) 后置递增一。等效于 counted_iterator temp{*this}; ++*this; return temp;。
4) 前置递减一。等效于 --current; ++length; return *this;。
5) 后置递减一。等效于 counted_iterator temp{*this}; --*this; return temp;。
6) 返回一个迭代器适配器,它被提前了 n。等效于 return counted_iterator(current + n, length - n);。
7) 将迭代器适配器提前 n。等效于 current += n; length -= n; return *this;。
8) 返回一个迭代器适配器,它被提前了 -n。等效于 return counted_iterator(current - n, length + n);。
9) 将迭代器适配器提前 -n。等效于 current -= n; length += n; return *this;。
目录 |
[编辑] 参数
n | - | 迭代器适配器要递增或递减的位置数 |
[编辑] 返回值
1) *this
2,3) 在更改之前创建的 *this 的副本。
4) *this
5) 在更改之前创建的 *this 的副本。
6) 一个提前了 n 的迭代器适配器。
7) *this
8) 一个提前了 -n 的迭代器适配器。
9) *this
[编辑] 示例
运行此代码
#include <cassert> #include <initializer_list> #include <iterator> int main() { const auto v = {1, 2, 3, 4, 5, 6}; std::counted_iterator<std::initializer_list<int>::iterator> it1{v.begin(), 5}; ++it1; assert(*it1 == 2 && it1.count() == 4); // (1) auto it2 = it1++; assert(*it2 == 2 && *it1 == 3); // (3) --it1; assert(*it1 == 2 && it1.count() == 4); // (4) auto it3 = it1--; assert(*it3 == 2 && *it1 == 1); // (5) auto it4 = it1 + 3; assert(*it4 == 4 && it4.count() == 2); // (6) auto it5 = it4 - 3; assert(*it5 == 1 && it5.count() == 5); // (8) it1 += 3; assert(*it1 == 4 && it1.count() == 2); // (7) it1 -= 3; assert(*it1 == 1 && it1.count() == 5); // (9) }
[编辑] 参见
(C++20) |
推进迭代器 (函数模板) |
(C++20) |
计算两个迭代器适配器之间的距离 (函数模板) |