std::counted_iterator<I>::counted_iterator
来自cppreference.com
< cpp | iterator | counted iterator
constexpr counted_iterator() requires std::default_initializable<I> = default; |
(1) | (C++20 起) |
constexpr counted_iterator( I x, std::iter_difference_t<I> n ); |
(2) | (C++20 起) |
template< class I2 > requires std::convertible_to<const I2&, I> |
(3) | (C++20 起) |
构造新的迭代器适配器。
2) 以 std::move(x) 初始化底层迭代器并以
n
初始化底层长度。若 n
为负则行为未定义。3) 以
other
的对应组分初始化底层迭代器与长度。参数
x | - | 要适配的迭代器 |
n | - | 到末尾的距离 |
other | - | 要转换的迭代器适配器 |
示例
运行此代码
#include <algorithm> #include <initializer_list> #include <iostream> #include <iterator> int main() { static constexpr auto pi = {3, 1, 4, 1, 5, 9, 2}; // (1) 默认构造函数: constexpr std::counted_iterator<std::initializer_list<int>::iterator> i1{}; static_assert(i1 == std::default_sentinel); static_assert(i1.count() == 0); // (2) 分别初始化迭代器与长度: constexpr std::counted_iterator<std::initializer_list<int>::iterator> i2{ pi.begin(), pi.size() - 2 }; static_assert(i2.count() == 5); static_assert(*i2 == 3 && i2[1] == 1); // (3) 转换构造函数: std::counted_iterator<std::initializer_list<const int>::iterator> i3{i2}; std::ranges::copy(i3, std::default_sentinel, std::ostream_iterator<const int>{std::cout, " "}); }
输出:
3 1 4 1 5
参阅
(C++20) |
赋值另一迭代器适配器 (公开成员函数) |