名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | ranges
 
 
範圍庫 (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 /* unspecified */ repeat = /* unspecified */;

}
(2) (C++23 起)
呼叫簽名 (Call signature)
template< class W >

    requires /* 見下文 */

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

    requires /* 見下文 */

constexpr /* see below */ 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) 表示式等價,適用於任何合適的子表示式 ef
3) 確定型別是否為 整型類 並具有可用的 差值型別

repeat_view 實現了 random_access_range。如果 Bound 不是 std::unreachable_sentinel_t,則 repeat_view 也實現了 sized_rangecommon_range

目錄

自定義點物件

名稱 views::repeat 表示一個 *自定義點物件*,它是一個常量 函式物件,屬於一個 字面量 semiregular 類型別。為便於說明,其型別的不帶 cv 限定版本表示為 __repeat_fn

所有 __repeat_fn 的例項都相等。在相同引數上呼叫不同 __repeat_fn 型別的例項的效果是等價的,無論表示該例項的表示式是左值還是右值,以及是否帶有 const 限定(但是,不要求可呼叫 volatile 限定的例項)。因此,views::repeat 可以自由複製,並且其副本可以互換使用。

給定一組型別 Args...,如果 std::declval<Args>()... 滿足上述 views::repeat 引數的要求,則 __repeat_fn 實現了

否則,__repeat_fn 的任何函式呼叫運算子都不參與過載解析。

[編輯] 資料成員

成員 定義
movable-box <W> value_ 檢視的重複元素
(僅用於闡釋的成員物件*)
Bound bound_ 哨兵值
(僅用於闡釋的成員物件*)

[編輯] 成員函式

建立一個 repeat_view
(公開成員函式)
獲取 repeat_view 的起始迭代器
(公開成員函式)
獲取表示 repeat_view 結束的哨兵
(公開成員函式)
如果 repeat_view 是有大小的,則獲取其大小
(公開成員函式)
繼承自 std::ranges::view_interface
返回派生檢視是否為空,僅在它滿足 sized_rangeforward_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 起)
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_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 起)
constexpr std::unreachable_sentinel_t end() const;
(2) (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>);
(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()
{
    // bounded overload
    for (auto s : std::views::repeat("C++"sv, 3))
        std::cout << s << ' ';
    std::cout << '\n';
 
    // unbounded overload
    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
(類模板) (自定義點物件)[編輯]
English 日本語 中文(简体) 中文(繁體)