elements_view
class (C++標準連結庫)
檢視範圍中每個 Tuple 類似值之選取索引處的專案。 例如,假設 有一個範圍 std::tuple<string, int>
,請建立檢視,其中包含每個 Tuple 中的 string
專案。
語法
template<input_range V, size_t N>
class elements_view : public view_interface<elements_view<V, N>>;
範本參數
N
要為檢視選取之專案的索引。
V
基礎範圍的型別。 這個類型必須滿足 ranges::input_range
。
檢視特性
如需下列專案的描述,請參閱 檢視類別特性
特性 | 描述 |
---|---|
範圍配接器 | views::elements |
基礎範圍 | 必須滿足 input_range 或更新版本 |
項目類型 | 與索引元組元素的類型相同 |
檢視反覆運算器類別 | forward_range 、 bidirectional_range 或 random_access_range |
大小 | 只有在基礎範圍滿足時才 sized_range |
為 const -iterable |
只有在基礎範圍滿足時才 const-iterable |
通用範圍 | 只有在基礎範圍滿足時才 common_range |
借用範圍 | 只有在基礎範圍滿足時才 borrowed_range |
成員
成員函式 | 說明 |
---|---|
建構函式C++20 | elements_view 建構 。 |
base C++20 |
取得基礎範圍。 |
begin C++20 |
取得第一個專案的反覆運算器。 |
end C++20 |
取得檢視結尾的 sentinel。 |
size C++20 |
取得此檢視中的項目數目。 基礎範圍必須滿足 sized_range 。 |
繼承自 view_interface |
說明 |
back C++20 |
取得最後一個專案。 |
empty C++20 |
測試 是否 elements_view 為空白。 |
front C++20 |
取得第一個專案。 |
operator[] C++20 |
取得位於指定位置的專案。 |
operator bool C++20 |
測試是否不是空的 elements_view 。 |
需求
標頭: <ranges>
(自C++20起)
命名空間:std::ranges
需要編譯程式選項:/std:c++20
或更新版本。
備註
您可以搭配 elements_view
使用的 Tuple 型態為 std::tuple
、 std::pair
與 std::array
。
建構函式
建構的 elements_view
實例。
1) constexpr elements_view(V base);
2) elements_view() requires std::default_initializable<V> = default;
參數
base
基礎範圍。
如需範本參數類型的相關信息,請參閱 範本參數。
傳回值
elements_view
執行個體。
備註
建立 elements_view
的最佳方式是使用 elements
範圍配接器。 範圍配接器是建立檢視類別的預定方式。 如果您想要建立自己的自定義檢視類型,則會公開檢視類型。
1) 從指定的檢視建立 elements_view
。
2) 預設建構 elements_view
。
範例: elements_view
// requires /std:c++20 or later
#include <array>
#include <iostream>
#include <map>
#include <ranges>
#include <vector>
#include <string>
#include <utility>
int main()
{
// ========== work with a std::map
std::map<std::string, int> cpp_standards
{
{"C++98", 1988},
{"C++03", 2003},
{"C++11", 2011},
{"C++14", 2014},
{"C++17", 2017},
{"C++20", 2020}
};
// create an elements_view of all the string elements (<1>) from each tuple
for (int const year : std::views::elements<1>(cpp_standards))
{
std::cout << year << ' '; // 2003 2011 2014 2017 1988 2020
}
std::cout << '\n';
// Another way to call the range adaptor using pipe (|) syntax
for (auto&& name : cpp_standards | std::views::elements<0>)
{
std::cout << name << ' '; // C++03 C++11 C++14 C++17 C++98 C++20
}
std::cout << '\n';
// ========== working with arrays
std::array<std::array<int, 4>, 3> arr = { {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}} };
for (int& fourth : arr | std::views::elements<3>)
{
std::cout << fourth << ' '; // 3 7 11
}
std::cout << '\n';
// ========== work with a std::pair
std::vector<std::pair<std::string, int>> windows
{
{"Windows 1.0", 1985},
{"Windows 2.0", 1987},
{"Windows 3.0", 1990},
{"Windows 3.1", 1992},
{"Windows NT 3.1", 1993},
{"Windows 95", 1995},
{"Windows NT 4.0", 1996},
{"Windows 98", 1998},
{"Windows 2000", 2000}
};
for (int year : std::views::elements<1>(windows))
{
std::cout << year << ' '; // 1985 1987 1990 1992 1993 1995 1996 1998 2000
}
}
2003 2011 2014 2017 1988 2020
C++03 C++11 C++14 C++17 C++98 c++20
3 7 11
1985 1987 1990 1992 1993 1995 1996 1998 2000
base
取得基礎範圍的複本。
// Uses a copy constructor to return the underlying range
constexpr V base() const& requires std::copy_constructible<V>;
// Uses a move constructor to return the underlying range
constexpr V base() &&;
參數
無。
傳回值
基礎範圍。
begin
取得 中第一個專案的 elements_view
反覆運算器。
1) constexpr auto begin() requires (!Simple_view<V>);
2) constexpr auto begin() const requires range<const V>;
參數
無。
傳回值
指向 中第一個專案的 elements_view
反覆運算器。
end
在結尾取得 sentinel elements_view
1) constexpr auto end() requires (!Simple_view<V> && !ranges::common_range<V>);
2) constexpr auto end() requires (!Simple_view<V> && ranges::common_range<V>);
3) constexpr auto end() const requires ranges::range<const V>;
4) constexpr auto end() const requires ranges::common_range<const V>;
參數
無。
傳回值
緊接在 中最後一個項目後面的 elements_view
sentinel:
size
取得檢視中的項目數目。
constexpr auto size() requires sized_range<V>;
constexpr auto size() const requires sized_range<const V>;
參數
無。
傳回值
elements_view
中的項目數。
備註
只有當基礎範圍是 sized_range
、或換句話說,限定時,才能使用檢視的大小。