winrt::map_base 结构模板 (C++/WinRT)

可从中派生的基类,用于实现自己的自定义不可观测关联集合。 有关详细信息和代码示例,请参阅 使用 C++/WinRT 的集合

语法

template <typename D, typename K, typename V>
struct map_base : map_view_base<D, K, V, winrt::impl::collection_version>

模板参数

typename D 派生的类型名称。

typename K 集合中键的类型。

typename V 集合中值的类型。

要求

支持的最低 SDK:Windows SDK 版本 10.0.17763.0 (Windows 10 版本 1809)

命名空间: winrt

标头: %WindowsSdkDir%IncludeWindowsTargetPlatformVersion<>\cppwinrt\winrt\base.h (默认包含)

成员函数

函数 说明
map_base::Clear 函数 map_base 对象中删除所有元素。
map_base::First 函数 检索表示map_base对象中第一个元素的 IIterator
map_base::GetView 函数 检索 map_base 对象的不可变视图。
map_base::HasKey 函数 确定指定的键是否属于 map_base 对象中的元素。
map_base::Insert 函数 map_base 对象中插入或更新元素。
map_base::Lookup 函数 查找由指定键标识的元素,并检索相应的值。
map_base::Remove 函数 map_base 对象中删除元素。
map_base::Size 函数 检索 map_base 对象中的元素数。

迭代器

map_base是一个范围,该范围由内部自由函数定义, (每个函数检索与标准语言功能兼容的迭代器) 。 因此,可以使用基于for范围的语句枚举map_base对象中的元素。

还可以从 map_base::First 函数检索 IIterator,并使用该函数循环访问map_base对象中的元素。

...
#include <iostream>
using namespace winrt;
using namespace Windows::Foundation::Collections;
...
struct MyMap :
    implements<MyMap, IMap<winrt::hstring, int>, IMapView<winrt::hstring, int>, IIterable<IKeyValuePair<winrt::hstring, int>>>,
    winrt::map_base<MyMap, winrt::hstring, int>
{
    auto& get_container() const noexcept
    {
        return m_values;
    }

    auto& get_container() noexcept
    {
        return m_values;
    }

private:
    std::map<winrt::hstring, int> m_values{
        { L"AliceBlue", 0xfff0f8ff }, { L"AntiqueWhite", 0xfffaebd7 }
    };
};
...
IMap<winrt::hstring, int> map{ winrt::make<MyMap>() };

for (auto const& el : map)
{
    std::wcout << el.Key().c_str() << L", " << std::hex << el.Value() << std::endl;
}

IIterator<IKeyValuePair<winrt::hstring, int>> it{ map.First() };
while (it.HasCurrent())
{
    std::wcout << it.Current().Key().c_str() << L", " << std::hex << it.Current().Value() << std::endl;
    it.MoveNext();
}

map_base::Clear 函数

map_base 对象中删除所有元素。

语法

void Clear() noexcept;

map_base::First 函数

检索表示map_base对象中第一个元素的 IIterator

语法

auto First();

返回值

一个 IIterator ,表示 map_base 对象中的第一个元素。

map_base::GetView 函数

检索 map_base 对象的不可变视图。

语法

winrt::Windows::Foundation::Collections::IMapView<K, V> GetView() const;

返回值

包含map_base不可变视图的 IMapView

map_base::HasKey 函数

确定指定的键是否属于 map_base 对象中的元素。

语法

bool HasKey(K const& key) const noexcept;

parameters

key 要查找的密钥。

返回值

true 如果找到包含键的元素,则为 ;否则为 false

map_base::Insert 函数

map_base 对象中插入或更新元素。

语法

bool Insert(K const& key, V const& value);

parameters

key 与要插入或更新的元素关联的键。

value 要插入或替换的值。

返回值

true 如果找到并更新了具有指定键的元素,则为 ;否则 false

map_base::Lookup 函数

查找由指定键标识的元素,并检索相应的值。

语法

V Lookup(K const& key) const;

parameters

key 要查找的键。

返回值

如果找到与要查找的键对应的值,则引发 winrt::hresult_out_of_bounds 异常。

map_base::Remove 函数

map_base 对象中删除元素。

语法

void Remove(K const& key);

parameters

key 与要删除的元素关联的键。

map_base::Size 函数

检索 map_base 对象中的元素数。

语法

uint32_t Size() const noexcept;

返回值

一个值,表示 map_base 对象中的元素数。

请参阅