std::counted_iterator<I>::operator++,+,+=,--,-,-=
From cppreference.com
< 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) 1ずつ事前インクリメントします。 ++current; --length; return *this; と同等です。
2) 1ずつ事後インクリメントします。 --length; try { return current++; } catch(...) { ++length; throw; } と同等です。
3) 1ずつ事後インクリメントします。 counted_iterator temp{*this}; ++*this; return temp; と同等です。
4) 1ずつ事前デクリメントします。 --current; ++length; return *this; と同等です。
5) 1ずつ事後デクリメントします。 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)
*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) |
2つのイテレータアダプタ間の距離を計算する (関数テンプレート) |