std::ranges::views::repeat, std::ranges::repeat_view

来自cppreference.com
< cpp‎ | ranges
 
 
范围库
范围适配器
 
 
在标头 <ranges> 定义
template<std::move_constructible W,

          std::semiregular Bound =std::unreachable_sentinel_t>
    requires (std::is_object_v<W>&&std::same_as<W, std::remove_cv_t<W>>&&
             (/*is-integer-like*/<Bound>||
              std::same_as<Bound, std::unreachable_sentinel_t>))

class repeat_view :publicranges::view_interface<repeat_view<W, Bound>>
(1) (C++23 起)
namespace views {

    inlineconstexpr/* 未指定 */ repeat =/* 未指定 */;

}
(2) (C++23 起)
调用签名
template<class W >

    requires /* 见下文 */

constexpr/* 见下文 */ repeat( W&& value );
(C++23 起)
template<class W, class Bound >

    requires /* 见下文 */

constexpr/* 见下文 */ repeat( W&& value, Bound&& bound );
(C++23 起)
辅助概念
concept /*integer-like-with-usable-difference-type*/=

    /*is-signed-integer-like*/<T>||

    (/*is-integer-like*/<T>&&std::weakly_incrementable<T>)
(3) (仅用于阐述*)
1) 通过重复产生相同的值来生成一系列元素的范围工厂。可以是有界或无界(无限)。
2) 对于任何合适的子表达式 efviews::repeat(e)views::repeat(e, f)表达式等价(有相同效果)于 repeat_view<std::decay_t<decltype((E))>>(e)repeat_view(e, f)
3) 确定类型是否为整数式类型并具有可用的差类型

repeat_view 实现 random_access_range。如果 Bound 不是 std::unreachable_sentinel_t,那么 repeat_view 会实现 sized_rangecommon_range

目录

定制点对象

名字 views::repeat 代表一个定制点对象,它是某个字面semiregular 类类型的 const 函数对象。 细节参见定制点对象(CustomizationPointObject)

[编辑]数据成员

成员 定义
movable-box <W>value_ 视图包含的重复元素
(仅用于阐述的成员对象*)
Boundbound_ 哨位值
(仅用于阐述的成员对象*)

[编辑]成员函数

创建一个 repeat_view
(公开成员函数)
获取 repeat_view 的起始迭代器
(公开成员函数)
获取代表 repeat_view 末尾的哨位
(公开成员函数)
如果有边界,获取 repeat_view 的大小
(公开成员函数)
继承自 std::ranges::view_interface
返回视图是否为空,仅当视图满足 forward_range 时提供
(std::ranges::view_interface<D> 的公开成员函数)[编辑]
(C++23)
返回指向范围起始的常量迭代器
(std::ranges::view_interface<D> 的公开成员函数)[编辑]
(C++23)
返回对应于范围常量迭代器的哨位
(std::ranges::view_interface<D> 的公开成员函数)[编辑]
返回派生视图是否为非空,仅当 ranges::empty 可应用于它时提供
(std::ranges::view_interface<D> 的公开成员函数)[编辑]
返回派生视图中的首元素,仅当视图满足 forward_range 时提供
(std::ranges::view_interface<D> 的公开成员函数)[编辑]
返回派生视图中的末元素,仅当视图满足 bidirectional_rangecommon_range 时提供
(std::ranges::view_interface<D> 的公开成员函数)[编辑]
返回派生视图中的第 n 个元素,仅当视图满足 random_access_range 时提供
(std::ranges::view_interface<D> 的公开成员函数)[编辑]

std::ranges::repeat_view::repeat_view

repeat_view() requires std::default_initializable<W>=default;
(1) (C++23 起)
constexprexplicit repeat_view(const W& value, Bound bound = Bound());
(2) (C++23 起)
constexprexplicit repeat_view( W&& value, Bound bound = Bound());
(3) (C++23 起)
template<class... WArgs, class... BoundArgs>

    requires std::constructible_from<W, WArgs...>
          &&std::constructible_from<Bound, BoundArgs...>
constexprexplicit
    repeat(std::piecewise_construct_t, std::tuple<WArgs...> value_args,

            std::tuple<BoundArgs...> bound_args =std::tuple<>{});
(4) (C++23 起)
1) 默认初始化 value_ 并值初始化 bound_ 。
2)value 初始化 value_ 并以 bound 初始化 bound_ 。
如果 Bound 不是 std::unreachable_sentinel_tbool(bound >=0)false,那么行为未定义。
3)std::move(value) 初始化 value_ 并以 bound 初始化 bound_ 。
如果 Bound 不是 std::unreachable_sentinel_tbool(bound >=0)false,那么行为未定义。
4)std::make_from_tuple<T>(std::move(value_args)) 初始化 value_ 并以 std::make_from_tuple<Bound>(std::move(bound_args)) 初始化 bound_ 。
如果 Bound 不是 std::unreachable_sentinel_tbool(bound >=0)false,那么行为未定义。

参数

value - 重复产生的值
bound - 边界
value_args - 包含 value_ 的初始化器的元组
bound_args - 包含 bound_ 的初始化器的元组

std::ranges::repeat_view::begin

constexpr/*iterator*/ begin()const;
(C++23 起)

返回 iterator (std::addressof(*value_ ))

std::ranges::repeat_view::end

constexpr/*iterator*/ end()const
    requires (!std::same_as<Bound, std::unreachable_sentinel_t>);
(1) (C++23 起)
constexprstd::unreachable_sentinel_t end()const;
(2) (C++23 起)
1) 返回 iterator (std::addressof(*value_ ), bound_ )

std::ranges::repeat_view::size

constexprauto size()const
    requires (!std::same_as<Bound, std::unreachable_sentinel_t>);
(C++23 起)

返回 to-unsigned-like (bound_ )

[编辑]推导指引

template<class W, class Bound =std::unreachable_sentinel_t>
repeat_view( W, Bound = Bound())-> repeat_view<W, Bound>;
(C++23 起)

[编辑]嵌套类

迭代器类型
(仅用于阐述的成员类*)

[编辑]注解

功能特性测试标准功能特性
__cpp_lib_ranges_repeat202207L(C++23)std::ranges::repeat_view

[编辑]示例

#include <iostream>#include <ranges>#include <string_view>usingnamespace std::literals;   int main(){// 有边界的重载for(auto s: std::views::repeat("C++"sv, 3))std::cout<< s <<' ';std::cout<<'\n';   // 无边界的重载for(auto s : std::views::repeat("我知道你知道的是"sv)| std::views::take(3))std::cout<< s <<' ';std::cout<<"...\n";}

输出:

C++ C++ C++ 我知道你知道的是我知道你知道的是我知道你知道的是 ...

[编辑]缺陷报告

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

缺陷报告 应用于 出版时的行为 正确行为
LWG 4053 C++20 views::repeat 的单实参调用不会退化实参 会退化实参
LWG 4054 C++20 repeat_view 调用 views::repeat 不会创建嵌套的 repeat_view 会创建嵌套的 repeat_view

[编辑]参阅

由通过重复对某个初值自增所生成的序列组成的 view
(类模板)(定制点对象)[编辑]
close