std::counted_iterator<I>::operator++,+,+=,--,-,-=
來自 cppreference.com
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) |
計算兩個迭代器介面卡之間的距離 (函式模板) |