std::ranges::views::repeat, std::ranges::repeat_view - cppreference.com
来自cppreference.com
| 在标头 |
||
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 : 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) 对于任何合适的子表达式 e 和 f,views::repeat(e) 和 views::repeat(e, f)表达式等价(有相同效果)于 repeat_view<std::decay_t<decltype((E))>>(e) 和 repeat_view(e, f)。
repeat_view 实现 random_access_range。如果 Bound 不是 std::unreachable_sentinel_t,那么 repeat_view 会实现 sized_range 和 common_range。
定制点对象
名字 views::repeat 代表一个定制点对象,它是某个字面 semiregular 类类型的 const 函数对象。 细节参见定制点对象 (CustomizationPointObject) 。
数据成员
| 成员 | 定义 |
movable-box <W> value_
|
视图包含的重复元素 (仅用于阐述的成员对象*) |
Bound bound_
|
哨位值 (仅用于阐述的成员对象*) |
成员函数
创建一个 repeat_view (公开成员函数) | |
获取 repeat_view 的起始迭代器 (公开成员函数) | |
获取代表 repeat_view 末尾的哨位 (公开成员函数) | |
如果有边界,获取 repeat_view 的大小 (公开成员函数) | |
继承自 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_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) | (C++23 起) |
constexpr explicit repeat_view( const W& value, Bound bound = Bound() ); |
(2) | (C++23 起) |
constexpr explicit 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...> constexpr explicit 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_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; |
(C++23 起) | |
std::ranges::repeat_view::end
constexpr /*iterator*/ end() const requires (!std::same_as<Bound, std::unreachable_sentinel_t>); |
(1) | (C++23 起) |
constexpr std::unreachable_sentinel_t end() const; |
(2) | (C++23 起) |
1) 返回 iterator (std::addressof(*value_ ), bound_ )。
2) 返回 std::unreachable_sentinel。
std::ranges::repeat_view::size
constexpr auto 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_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("我知道你知道的是"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
|