共用方式為


take_while_view class (C++標準連結庫)

檢視,包含符合述詞之範圍的前置元素。

語法

template<view V, class Pred> requires
  input_range<V> && is_object_v<Pred> &&
  indirect_unary_predicate<const Pred, iterator_t<V>>
class take_while_view : public view_interface<take_while_view<V, Pred>>;

範本參數

Pred
述詞的類型,決定要放入檢視中的前置元素。

V
基礎檢視的類型。

檢視特性

如需下列專案的描述,請參閱 檢視類別特性

特性 描述
範圍配接器 views::take_while
基礎範圍 必須滿足 input_range 或更新版本
項目類型 與基礎範圍相同
檢視反覆運算器類別 與基礎範圍相同
大小 No
const-iterable 只有當基礎範圍是 const 可反覆運算的,述詞才能使用 const 參考。
通用範圍 No
借用範圍 No

成員

成員函式 說明
建構函式C++20 建構檢視。
baseC++20 取得基礎範圍。
beginC++20 取得第一個專案的反覆運算器。
endC++20 取得檢視結尾的 sentinel。
predC++20 取得述詞的參考,這個述詞會決定要採用哪些元素。
繼承自 view_interface 說明
backC++20 取得最後一個專案。
dataC++20 取得第一個專案的指標。
emptyC++20 測試檢視是否為空白。
frontC++20 取得第一個專案。
operator[]C++20 取得位於指定位置的專案。
operator boolC++20 測試檢視是否不是空的。
size 取得檢視中的項目數目。

需求

標頭: <ranges> (自C++20起)

命名空間std::ranges

需要編譯程式選項:/std:c++20或更新版本。

建構函式

建構的實例 take_while_view

1) take_while_view() requires 
    default_initializable<V> &&
    default_initializable<Pred> = default;

2) constexpr take_while_view(V base, Pred pred);

參數

base
基礎檢視。

pred
決定要放入檢視之前置元素的述詞。

如需範本參數類型的相關信息,請參閱 範本參數

傳回值

take_while_view 物件。

備註

建立 take_while_view 的最佳方式是使用 views::take_while 範圍配接器。 範圍配接器是建立檢視類別的預定方式。 如果您想要建立自己的自定義檢視類型,則會公開檢視類型。

1) 移動會 take_while_viewbase 檢視和 pred 述詞建構 。 和 pred 都會base透過std::move()移動。
2) 建構空 take_while_view的 。 基礎檢視和述詞是預設建構的。

範例: take_while_view

// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v{0, 1, 2, 3, -4, 5, 6};
    auto twv = std::views::take_while(v, [](int i) {return i >= 0; });
    
    for (auto& e : twv)
    {
        std::cout << e << ' '; // 0 1 2 3
    }
    std::cout << '\n';

    // Using the '|' operator to create a take_view
    for (auto i : v | std::views::take_while([](int i) {return i < 5; }))
    {
        std::cout << i << ' '; // 0 1 2 3 -4
    }
}
0 1 2 3
0 1 2 3 -4

base

取得基礎檢視的複本。

// Uses a copy constructor to return the underlying view
1) constexpr V base() const& requires std::copy_constructible<V>;

// Uses a move constructor to return the underlying view
2) constexpr V base() &&;

參數

無。

傳回

基礎檢視的複本。

begin

取得檢視中第一個專案的反覆運算器。

1) constexpr auto begin() requires (!Simple_view<V>);
2) constexpr auto begin() const requires
        range<const V> && 
        indirect_unary_predicate<const Pred, iterator_t<const V>>

參數

無。

傳回值

指向檢視中第一個專案的反覆運算器。 如果檢視沒有述詞,則行為是未定義的。

具有元素 10、20 和 30 的向量圖片。第一個元素包含 10,且標示為 begin()。最後一個專案包含 30 個,且標示為「最後一個專案」。最後一個專案後面的虛方塊表示 sentinel 且標示為 end()。

備註

對於 1,需求 Simple_view 表示檢視 Vconst V 具有相同反覆運算器和 sentinel 類型。

end

取得檢視結尾的 sentinel。

1) constexpr auto end() requires (!Simple_view<V>);
2) constexpr auto end() const requires
        range<const V> &&
        indirect_unary_predicate<const Pred, iterator_t<const V>

參數

無。

傳回值

檢視中最後一個專案後面的 sentinel。

具有元素 10、20 和 30 的向量圖片。第一個元素包含 10,且標示為 begin()。最後一個專案包含 30 個,且標示為「最後一個專案」。最後一個專案後面的虛方塊表示 sentinel 且標示為 end()。

備註

對於 1,需求 Simple_view 表示檢視 Vconst V 具有相同反覆運算器和 sentinel 類型。

pred

取得述詞的參考,這個述詞用來選取哪些前置元素將在檢視中執行。

constexpr const Pred& pred() const;

傳回值

述詞的參考,用來選取要放入檢視中的前置元素。

pred

// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v{ 0, 1, 2, 3, -4, 5, 6 };
    auto mv = v | std::views::take_while(
        [](int i) {return i < 5; });
    std::cout << std::boolalpha << mv.pred()(v[6]); // outputs false because v[6] = 6 and 6 is not less than 5 (the predicate)
}

另請參閱

<ranges>
take_view
take_while 範圍配接器
檢視類別