Namespaces
Variants

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

From cppreference.net
Ranges library
Range adaptors
定义于头文件 <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 >> &&
( /*integer-like-with-usable-difference-type*/ < Bound > ||
std:: same_as < Bound, std:: unreachable_sentinel_t > ) )

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

inline constexpr /* 未指定 */ 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) views :: repeat ( e ) views :: repeat ( e, f ) 分别与 repeat_view < std:: decay_t < decltype ( ( E ) ) >> ( e ) repeat_view ( e, f ) 表达式等价 的意义上等价,适用于任何合适的子表达式 e f
3) 确定一个类型是否为 类整数类型 且具有可用的 差值类型

repeat_view 满足 random_access_range 概念。若 Bound 不是 std::unreachable_sentinel_t ,则 repeat_view 还满足 sized_range common_range 概念。

目录

定制点对象

名称 views::repeat 表示一个 定制点对象 ,这是一个常量 函数对象 ,具有 字面量 类型的 semiregular 类类别。有关详细信息,请参阅 定制点对象

数据成员

成员 定义
movable-box  < W > value_ 视图的重复元素
( 仅用于说明的成员对象* )
Bound bound_ 哨兵值
( 仅用于说明的成员对象* )

成员函数

创建 repeat_view
(公开成员函数)
获取 repeat_view 的起始迭代器
(公开成员函数)
获取表示 repeat_view 末尾的哨位
(公开成员函数)
获取 repeat_view 的大小(若其具有大小)
(公开成员函数)
继承自 std::ranges::view_interface
返回派生视图是否为空(仅在满足 sized_range 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_range common_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) (since C++23)
constexpr explicit repeat_view ( const W & value, Bound bound = Bound ( ) ) ;
(2) (since C++23)
constexpr explicit repeat_view ( W && value, Bound bound = Bound ( ) ) ;
(3) (since C++23)
template < class ... WArgs , class ... BoundArgs >

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

std:: tuple < BoundArgs... > bound_args = std:: tuple <> { } ) ;
(4) (since C++23)
1) 默认初始化 value_ 并值初始化 bound_ 。
2) 使用 value 初始化 value_ ,并使用 bound 初始化 bound_
Bound 不是 std::unreachable_sentinel_t bool ( bound >= 0 ) false ,则行为未定义。
3) 使用 std :: move ( value ) 初始化 value_ ,并使用 bound 初始化 bound_
Bound 不是 std::unreachable_sentinel_t bool ( 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_t bool ( bound >= 0 ) false ,则行为未定义。

参数

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

std::ranges::repeat_view:: begin

constexpr /*iterator*/ begin ( ) const ;
(since 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) (since C++23)
constexpr std:: unreachable_sentinel_t end ( ) const ;
(2) (since C++23)
1) 返回 iterator  ( std:: addressof ( * value_  ) , bound_  )

std::ranges::repeat_view:: size

constexpr auto size ( ) const
requires ( ! std:: same_as < Bound, std:: unreachable_sentinel_t > ) ;
(since 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_repeat 202207L (C++23) std::ranges::repeat_view

示例

#include <iostream>
#include <ranges>
#include <string_view>
using namespace 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("I know that you know that"sv)
                | std::views::take(3))
        std::cout << s << ' ';
    std::cout << "...\n";
}

输出:

C++ C++ C++
I know that you know that I know that you know that I know that you know that ...

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用标准 发布时行为 正确行为
LWG 4053 C++20 views::repeat 的单参数调用未对参数进行退化处理 对参数进行退化处理
LWG 4054 C++20 使用 repeat_view 调用 views::repeat
未创建嵌套的 repeat_view
创建嵌套的
repeat_view

参见

由重复递增初始值生成的序列组成的 view
(类模板) (定制点对象)