std::counted_iterator<I>::operator++,+,+=,--,-,-=
来自cppreference.com
<tbody>
</tbody>
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)
*this2,3) 更改前创建的
*this 的副本。4)
*this5) 更改前创建的
*this 的副本。6) 前进
n 的迭代器适配器。7)
*this8) 前进
-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) |
计算两个迭代器适配器间的距离 (函数模板) |