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