winrt::map_base struct template (C++/WinRT)
A base class from which you can derive to implement your own custom non-observable associative collection. For more info, and code examples, see Collections with C++/WinRT.
template <typename D, typename K, typename V>
struct map_base : map_view_base<D, K, V, winrt::impl::collection_version>
typename D
Your derived type name.
typename K
The type of the keys in the collection.
typename V
The type of the values in the collection.
Minimum supported SDK: Windows SDK version 10.0.17763.0 (Windows 10, version 1809)
Namespace: winrt
Header: %WindowsSdkDir%Include<WindowsTargetPlatformVersion>\cppwinrt\winrt\base.h (included by default)
Function | Description |
---|---|
map_base::Clear function | Removes all elements from the map_base object. |
map_base::First function | Retrieves an IIterator representing the first element in the map_base object. |
map_base::GetView function | Retrieves an immutable view of the map_base object. |
map_base::HasKey function | Determines whether the specified key belongs to an element in the map_base object. |
map_base::Insert function | Inserts or updates an element in the map_base object. |
map_base::Lookup function | Looks up the element identified by the specified key, and retrieves the corresponding value. |
map_base::Remove function | Removes an element from the map_base object. |
map_base::Size function | Retrieves the number of elements in the map_base object. |
A map_base is a range, and that range is defined by internal free functions (each of which retrieves an iterator) that are compatible with standard language features. Because of this, you can enumerate the elements in a map_base object with a range-based for
statement.
You can also retrieve an IIterator from the map_base::First function, and use that to iterate through the elements in a map_base object.
...
#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();
}
Removes all elements from the map_base object.
void Clear() noexcept;
Retrieves an IIterator representing the first element in the map_base object.
auto First();
An IIterator representing the first element in the map_base object.
Retrieves an immutable view of the map_base object.
winrt::Windows::Foundation::Collections::IMapView<K, V> GetView() const;
An IMapView containing an immutable view of the map_base.
Determines whether the specified key belongs to an element in the map_base object.
bool HasKey(K const& key) const noexcept;
key
The key to look for.
true
if an element containing the key is found, otherwise false
.
Inserts or updates an element in the map_base object.
bool Insert(K const& key, V const& value);
key
The key associated with the element to insert or update.
value
The value to insert or replace.
true
if an element with the specified key was found and updated; otherwise false
.
Looks up the element identified by the specified key, and retrieves the corresponding value.
V Lookup(K const& key) const;
key
The key to look up.
The value corresponding to the key being looked up if found, otherwise a winrt::hresult_out_of_bounds exception is thrown.
Removes an element from the map_base object.
void Remove(K const& key);
key
The key associated with the element to remove.
Retrieves the number of elements in the map_base object.
uint32_t Size() const noexcept;
A value representing the number of elements in the map_base object.