std::counted_iterator<I>:: operator++,+,+=,--,-,-=
From cppreference.net
<
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)
|
计算两个迭代器适配器间的距离
(函数模板) |