std::ranges::iota_view<W, Bound>::iota_view

来自cppreference.com
< cpp‎ | ranges‎ | iota view
 
 
范围库
范围适配器
 
 
iota_view() requires std::default_initializable<W> = default;
(1)(C++20 起)
constexpr explicit iota_view( W value );
(2)(C++20 起)
constexpr explicit iota_view( std::type_identity_t<W> value,
                              std::type_identity_t<Bound> bound );
(3)(C++20 起)
constexpr explicit iota_view( /*iterator*/ first, /* 见下文 */ last );
(4)(C++20 起)

构造 iota_view

 重载 数据成员
value_bound_
(1)值初始化值初始化
(2)value 初始化
(3) 以 bound 初始化 
(4) 以 first.value_ 初始化 见下文
2,3) 如果满足以下任意条件,那么行为未定义:
  • Bound()value 不可及,除非 Bound 表示 std::unreachable_sentinel_t
  • WBound 实现了 totally_ordered_with,并且 bool(value <= bound)false
4) 如果满足以下任意条件,那么行为未定义:
  • Bound()value 不可及,除非 Bound 表示 std::unreachable_sentinel_t
  • WBound 实现了 totally_ordered_with,并且 bool(first.value_ <= bound)false
last 的类型和 bound_ 的初始化方式由 Bound 表示的类型确定:
Bound 表示的类型 last 的类型 bound_
Witerator 以 last.value_ 初始化 
 std::unreachable_sentinel_t Boundlast 初始化
其他类型sentinellast.bound_ 初始化

参数

value-起始值
bound-边界
first-代表起始值的迭代器
last-代表边界的迭代器或哨位

示例

#include <cassert>
#include <iostream>
#include <iterator>
#include <ranges>
 
int main()
{
    const auto l = {1, 2, 3, 4};
 
    auto i1 = std::ranges::iota_view<int, int>(); // 重载 (1)
    assert(i1.empty() and i1.size() == 0);
 
    auto i2 = std::ranges::iota_view(1); // 重载 (2)
    assert(not i2.empty() and i2.front() == 1);
    for (std::cout << "1) "; auto e : i2 | std::views::take(3))
        std::cout << e << ' ';
    std::cout << '\n';
 
    auto i3 = std::ranges::iota_view(std::begin(l)); // 重载 (2)
    assert(not i3.empty() and i3.front() == l.begin());
    for (std::cout << "2) "; auto e : i3 | std::views::take(4))
        std::cout << *e << ' ';
    std::cout << '\n';
 
    auto i4 = std::ranges::iota_view(1, 8); // 重载 (3)
    assert(not i4.empty() and i4.front() == 1 and i4.back() == 7);
    for (std::cout << "3) "; auto e : i4)
        std::cout << e << ' ';
    std::cout << '\n';
 
    auto i5 = std::ranges::iota_view(l.begin(), l.end()); // 重载 (4)
    for (std::cout << "4) "; auto e : i5)
        std::cout << *e << ' ';
    std::cout << '\n';
 
    auto i6 = std::ranges::iota_view(l.begin(), std::unreachable_sentinel); // (4)
    for (std::cout << "5) "; auto e : i6 | std::views::take(3))
        std::cout << *e << ' ';
    std::cout << '\n';
}

输出:

1) 1 2 3
2) 1 2 3 4
3) 1 2 3 4 5 6 7
4) 1 2 3 4
5) 1 2 3

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告应用于出版时的行为正确行为
LWG 3523C++20重载 (4) 可能使用错误的哨位类型已更正
P2711R1C++20重载 (3,4) 不是显式的改成显式的