共用方式為


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

檢視集合中每個類似 Tuple 的值的第一個索引。 例如,假設 有一個範圍 std::tuple<string, int>,請建立檢視,其中包含每個 Tuple 中的所有 string 專案。

keys_view是 的elements_view<R, 0>別名,適合從 或 std::unordered_mapstd::map關聯容器檢視索引鍵。

語法

template<input_range R>
using keys_view = ranges::elements_view<R, 0>;

範本參數

R
基礎範圍的型別。 這個類型必須滿足 ranges::input_range

檢視特性

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

特性 描述
範圍配接器 views::keys
基礎範圍 必須滿足 input_range 或更新版本
項目類型 與基礎範圍之第一個 Tuple 元素的類型相同
檢視反覆運算器類別 random_access_range 如果基礎範圍是連續的,則為 ,否則與基礎範圍相同
大小 只有在基礎範圍滿足時才 sized_range
const-iterable 只有在基礎範圍滿足時才 const-iterable
通用範圍 只有在基礎範圍滿足時才 common_range
借用範圍 只有在基礎範圍滿足時才 borrowed_range

成員

下列成員函式清單是指 類別 keys_view 。 回想一 element_view 下,這是類別範本具現化的別名。

成員函式 說明
建構函式C++20 keys_view建構 。
baseC++20 取得基礎範圍。
beginC++20 取得第一個專案的反覆運算器。
endC++20 取得檢視結尾的 sentinel。
sizeC++20 取得項目數目。 基礎範圍必須滿足 sized_range
繼承自 view_interface 說明
backC++20 取得最後一個專案。
emptyC++20 測試 是否 keys_view 為空白。
frontC++20 取得第一個專案。
operator[]C++20 取得位於指定位置的專案。
operator boolC++20 測試是否不是空的 keys_view

需求

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

命名空間std::ranges

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

備註

您可以搭配 keys_view 使用的 Tuple 型態為 std::tuplestd::pairstd::array

建構函式

建構的 keys_view實例。

1) constexpr keys_view(V base);
2) keys_view() requires std::default_initializable<V> = default;

參數

base
基礎範圍。

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

傳回值

keys_view 執行個體。

備註

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

1) 從指定的檢視建立 keys_view
2) 預設建構函式會建立空 keys_view的 。

範例: keys_view

// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <map>
#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}
    };

    // Extract all of the keys from the map
    for (const std::string& standards : std::views::keys(cpp_standards))
    {
        std::cout << standards << ' '; // C++03 C++11 C++14 C++17 C++98 C++20
    }
    std::cout << '\n';

    // ========== work with a range of 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 an keys_view from each pair
    for (const std::string& version : windows | std::views::keys)
    {
        std::cout << version << ' '; // Windows 1.0 Windows 2.0 Windows 3.0 ...
    }
}
C++03 C++11 C++14 C++17 C++98 c++20
Windows 1.0 Windows 2.0 Windows 3.0 Windows 3.1 Windows NT 3.1 Windows 95 Windows NT 4.0 Windows 95 Windows 98 Windows 1.0 Windows 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

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

1) constexpr auto begin() requires (!Simple_view<V>);
2) constexpr auto begin() const requires (Simple_view<V>) // or put another way, requires ranges::range<const V>;

參數

無。

傳回值

指向 中第一個專案的 keys_view反覆運算器。

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

end

在結尾取得 sentinel keys_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>;

參數

無。

傳回值

緊接在 中最後一個項目後面的 keys_viewsentinel:

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

size

取得項目數目。

constexpr auto size() requires sized_range<V>
constexpr auto size() const requires sized_range<const V>

參數

無。

傳回值

keys_view 中的項目數。

備註

只有當基礎範圍是 sized_range、或換句話說,限定時,才能使用檢視的大小。

另請參閱

elements_view
values_view
<ranges>
檢視類別