名稱空間
變體
操作

std::counted_iterator<I>::operator++,+,+=,--,-,-=

來自 cppreference.com
 
 
迭代器庫
迭代器概念
迭代器原語
演算法概念與工具
間接可呼叫概念
常用演算法要求
(C++20)
(C++20)
(C++20)
工具
(C++20)
迭代器介面卡
範圍訪問
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
std::counted_iterator
成員函式
counted_iterator::operator++counted_iterator::operator++(int)counted_iterator::operator+counted_iterator::operator+=counted_iterator::operator--counted_iterator::operator--(int)counted_iterator::operator-counted_iterator::operator-=
非成員函式
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
輔助類
 
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)
計算兩個迭代器介面卡之間的距離
(函式模板) [編輯]