std::ranges::adjacent_view<V,N>::iterator<Const>::operator++,--,+=,-=
来自cppreference.com
| |
(1) | (C++23 起) |
| |
(2) | (C++23 起) |
| |
(3) | (C++23 起) |
| |
(4) | (C++23 起) |
| |
(5) | (C++23 起) |
| |
(6) | (C++23 起) |
递增或递减迭代器。
令 current_ 为底层迭代器数组。
1) 等价于:
如果在调用之前,
for (auto& i : current_)
i = std::ranges::next(i);
return *this;
current_.back() 不可递增,则行为未定义。2) 等价于:
auto tmp = *this;
++*this;
return tmp;
3) 等价于:
如果调用之前,
for (auto& i : current_)
i = std::ranges::prev(i);
return *this;
current_.front() 不可递减,则行为未定义。4) 等价于:
auto tmp = *this;
--*this;
return tmp;
5) 等价于:
如果调用之前,
for (auto& i : current_)
i = i += n;
return *this;
current_.back() + n 无良定义的行为,则行为未定义。6) 等价于:
如果调用之前,
for (auto& i : current_)
i = i -= n;
return *this;
current_.front() - n 无良定义的行为,则行为未定义。参数
| n | - | 相对于当前位置的偏移量 |
返回值
1,3,5,6)
*this。2,4)
*this 被修改前的副本。示例
Run this code
#include <cassert>
#include <list>
#include <ranges>
#include <utility>
#include <vector>
int main()
{
{
auto v = std::vector{0, 1, 2, 3, 4, 5};
auto i = (v | std::views::pairwise).begin();
assert((*i == std::pair{0, 1}));
++i; // 重载 (1)
assert((*i == std::pair{1, 2}));
--i; // 重载 (3)
assert((*i == std::pair{0, 1}));
i += 2; // 重载 (5)
assert((*i == std::pair{2, 3}));
i -= 2; // 重载 (6)
assert((*i == std::pair{0, 1}));
}
{
auto v = std::list{0, 1, 2, 3, 4, 5};
auto i = (v | std::views::pairwise).begin();
assert((*i == std::pair{0, 1}));
++i; // 重载 (1)
assert((*i == std::pair{1, 2}));
--i; // 重载 (3)
assert((*i == std::pair{0, 1}));
// i += 2; // 错误: v 不是 random_access_range; 重载 (5)
// i -= 2; // 错误: v 不是 random_access_range; 重载 (6)
}
}
参阅
(C++23) |
进行迭代器算数 (公开成员函数) |