transform_view
class (C++標準連結庫)
項目的檢視,每個檢視都是指定範圍中元素的轉換。
語法
template<input_range V, move_constructible F>
requires view<V> && is_object_v<F> &&
regular_invocable<F&, range_reference_t<V>> &&
can-reference<invoke_result_t<F&, range_reference_t<V>>>
class transform_view : public view_interface<transform_view<V, F>>;
範本參數
F
轉換專案之函式物件的型別。
V
基礎檢視的類型。
檢視特性
如需下列專案的描述,請參閱 檢視類別特性
特性 | 描述 |
---|---|
範圍配接器 | views::transform |
基礎範圍 | 必須滿足 input_range 或更新版本 |
項目類型 | 與轉換函式的傳回型別相同。 |
檢視反覆運算器類別 | input_range 支援最多 random_access_range ,視基礎範圍而定 |
大小 | 只有在基礎範圍滿足時才 sized_range |
為 const -iterable |
只有當基礎範圍是 const 可反覆運算的,且轉換適用於參考時 const 。 |
通用範圍 | 只有在基礎範圍滿足時才 common_range |
借用範圍 | No |
成員
成員函式 | 說明 |
---|---|
建構函式C++20 | 建構檢視。 |
base C++20 |
取得基礎範圍。 |
begin C++20 |
取得第一個專案的反覆運算器。 |
end C++20 |
取得檢視結尾的 sentinel。 |
size C++20 |
取得項目數目。 基礎範圍必須滿足 sized_range 。 |
繼承自 view_interface |
說明 |
back C++20 |
取得最後一個專案。 |
empty C++20 |
測試檢視是否為空白。 |
front C++20 |
取得第一個專案。 |
operator bool C++20 |
測試檢視是否不是空的。 |
operator[] C++20 |
取得位於指定位置的專案。 |
需求
標頭: <ranges>
(自C++20起)
命名空間:std::ranges
需要編譯程式選項:/std:c++20
或更新版本。
建構函式
建構的實例 transform_view
1) transform_view() requires default_initializable<V>
&& default_initializable<F> = default;
2) constexpr transform_view(V base, F func);
參數
base
基礎檢視。
func
轉換每個專案的函式。
如需範本參數類型的相關信息,請參閱 範本參數。
傳回值
transform_view
執行個體。
備註
建立 transform_view
的最佳方式是使用 views::transform
範圍配接器。 範圍配接器是建立檢視類別的預定方式。 如果您想要建立自己的自定義檢視類型,則會公開檢視類型。
1) 建立值初始化 transform_view
的 。 轉換函式和基礎檢視必須是預設的可初始化。
2) 從base
檢視和轉換函式 func
移動 建構 transform_view
。 和 func
都會base
透過std::move()
移動。
範例: transform_view
// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>
#include <chrono>
using namespace std;
using namespace chrono;
void print(auto v)
{
for (auto x : v)
{
cout << x << ' ';
}
cout << '\n';
}
struct classes
{
string className;
weekday startDay;
};
int main()
{
std::vector<int> v{0, 1, 2, 3, -4, 5, 6};
// outputs 0 2 4 6 -8 10 12
print(v | std::views::transform([](int i) {return i * 2; }));
// ---- Modify the elements in the collection by returning a reference to the element to transform
std::vector<classes> theClasses = {
{"Math", Monday},
{"English", Wednesday},
{"History", Monday},
{"Science", Wednesday},
{"Art", Friday},
{"Music", Thursday}
};
// lambda to get a reference to the day of the week for a class
auto getDay = [](classes& c) -> weekday&
{
return c.startDay;
};
// If a class starts on Monday, change it to Tuesday
for (auto&& startDay : theClasses | std::views::transform(getDay))
{
// modify the startDay in the collection
if (startDay == Monday)
{
startDay = Tuesday;
}
}
// output classes and start times
for (auto c : theClasses)
{
std::cout << c.className << " : " << c.startDay << '\n';
}
}
0 2 4 6 -8 10 12
Math : Tue
English : Wed
History : Tue
Science : Wed
Art : Fri
Music : Thu
base
取得基礎檢視。
// Uses a copy constructor to return the underlying view
constexpr V base() const& requires std::copy_constructible<V>;
// Uses std::move() to return the underlying view
constexpr V base() &&;
參數
無。
傳回
基礎檢視。
begin
取得檢視中第一個專案的反覆運算器。
constexpr auto begin();
傳回值
指向檢視中第一個專案的反覆運算器。 如果檢視沒有述詞,則行為是未定義的。
end
取得檢視結尾的 sentinel。
constexpr auto end()
傳回值
檢視中最後一個專案後面的 sentinel:
size
取得檢視中的項目數目。
constexpr auto size() requires ranges::sized_range<V>;
constexpr auto size() const requires ranges::sized_range<const V>;
參數
無。
傳回值
檢視中的項目數目。