std::ranges::views::iota, std::ranges::iota_view

来自cppreference.com
< cpp‎ | ranges
 
 
 
 
template<std::weakly_incrementable W,

         std::semiregular Bound = std::unreachable_sentinel_t>
    requires __WeaklyEqualityComparableWith<W, Bound> && std::semiregular<W>

class iota_view : public ranges::view_interface<iota_view<W, Bound>>
(1) (C++20 起)
namespace views {

    inline constexpr /*unspecified*/ iota = /*unspecified*/;

}
(2) (C++20 起)
1) 以重复自增初值生成序列的范围工厂。能为有界或无界(无限)。
2) views::iota(E)views::iota(E, F) 对于适合的 EF 子表达式分别表达式等价iota_view{E}iota_view{E, F}

表达式等价

表达式 e 表达式等价于表达式 f ,若 ef 拥有相同效果,均为潜在抛出或均非潜在抛出(即 noexcept(e) == noexcept(f) ),且均为常量子表达式或均非常量子表达式。

辅助模板

template<std::weakly_incrementable W, std::semiregular Bound>
inline constexpr bool enable_borrowed_range<ranges::iota_view<W, Bound>> = true;

std::ranges::enable_borrowed_range 的此特化使得 iota_view 满足 borrowed_range

数据成员

std::ranges::iota_view::base_

W value_ = W(); /* exposition-only */

当前值

std::ranges::iota_view::bound_

Bound bound_ = Bound(); /* exposition-only */

边界(默认为 std::unreachable_sentinel_t

成员函数

std::ranges::iota_view::iota_view

iota_view() = default;
(1)
constexpr explicit iota_view(W value);
(2)
constexpr iota_view(std::type_identity_t<W> value,
                    std::type_identity_t<Bound> bound);
(3)
1) 值初始化 value_bound_
2)value 初始化 value_ 。此构造函数用于创建无界 iota_view ,例如 iota(0) 产生数 0、 1、 2 ……无穷大。
3)value 初始化 value_ 并以 bound 初始化 bound_ 。若 std::totally_ordered_with<W, Bound> 得到实现且 bool(value <= bound)false 则行为未定义。此构造函数用于创建有界 iota_view ,例如 iota(10, 20) 产生从 10 到 19 的数。

对于 (2)(3) ,若 iota_view 有界(即 Bound 不是 std::unreachable_sentinel_t )且初始化 bound_ 为不可从 value 抵达的值,则行为未定义。

参数

value - 开始值
bound - 边界

std::ranges::iota_view::begin

constexpr /*iterator*/ begin() const;

返回以 value_ 初始化的迭代器

std::ranges::iota_view::end

constexpr auto end() const;
(1)
constexpr /*iterator*/ end() const requires std::same_as<W, Bound>;
(2)
1) 若视图有界则返回以 bound_ 初始化的特定类型的哨位,或若视图无界则返回 std::unreachable_sentinel
2) 返回以 bound_ 初始化的迭代器

std::ranges::iota_view::size

constexpr auto size() const

  requires (std::same_as<W, Bound> && __Advanceable<W>) ||
           (std::integral<W> && std::integral<Bound>) ||
             std::sized_sentinel_for<Bound, W>
{
  if constexpr (__is_integer_like<W> && __is_integer_like<Bound>)
    return (value_ < 0)
      ? ((bound_ < 0)
        ? __make_unsigned_like(-value_) - __make_unsigned_like(-bound_)
        : __make_unsigned_like(bound_) + __make_unsigned_like(-value_))
      : __make_unsigned_like(bound_) - __make_unsigned_like(value_);
  else
    return __make_unsigned_like(bound_ - value_);

}

若视图有界则返回视图的大小。

推导指引

template<class W, class Bound>

    requires (!__is_integer_like<W> || !__is_integer_like<Bound> ||
              __is_signed_integer_like<W> == __is_signed_integer_like<Bound>)

  iota_view(W, Bound) -> iota_view<W, Bound>;

注意推导指引自身抵御有符号/无符号不匹配漏洞,如 views::iota(0, v.size()) ,其中 0 为(有符号) intv.size() 为(无符号) std::size_t

嵌套类

迭代器类型
(公开成员类)
iota_view 有界且 BoundW 不是同一类型时使用的哨位类型
(公开成员类)

示例

#include <ranges>
#include <iostream>
 
int main()
{
    for (int i : std::iota_view{1, 10})
        std::cout << i << ' ';
    std::cout << '\n';
 
    for (int i : std::views::iota(1, 10))
        std::cout << i << ' ';
    std::cout << '\n';
 
    for (int i : std::views::iota(1) | std::views::take(9))
        std::cout << i << ' ';
    std::cout << '\n';
}

输出:

1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9