template<class key_t, class value_t, class hasher_t = xtd::collections::generic::helpers::hasher<key_t>, class equator_t = xtd::collections::generic::helpers::equator<key_t>, class allocator_t = xtd::collections::generic::helpers::allocator<std::pair<const key_t, value_t >>>
class xtd::collections::generic::dictionary< key_t, value_t, hasher_t, equator_t, allocator_t >
Represents a collection of keys and values.
- Definition
dictionary() noexcept=default
Initializes a new instance of the xtd::collections::generic::dictionary <key_t, value_t> class that i...
Represents a generic collection of key/value pairs.
Definition idictionary.hpp:44
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
std::allocator< type_t > allocator
Represent an allocator alias.
Definition allocator.hpp:38
Implements a function object for performing comparisons. Unless specialised, invokes operator== on ty...
Definition equator.hpp:39
- Header
#include <xtd/collections/generic/dictionary>
- Namespace
- xtd::collections::generic
- Library
- xtd.core
- Examples
- The following code example creates an empty xtd::collections::generic::dictionary <key_t, value_t> of strings with string keys and uses the Add method to add some elements. The example demonstrates that the xtd::collections::generic::dictionary::add method throws an xtd::argument_exception when attempting to add a duplicate key.
The example uses the xtd::collections::generic::dictionary::operator [] to retrieve values, demonstrating that a xtd::collections::generic::key_not_found_exception is thrown when a requested key is not present, and showing that the value associated with a key can be replaced.
The example shows how to use the xtd::collections::generic::dictionary::try_get_vValue method as a more efficient way to retrieve values if a program often must try key values that are not in the dictionary, and it shows how to use the xtd::collections::generic::dictionary::contains_key method to test whether a key exists before calling the xtd::collections::generic::dictionary::add method.
The example shows how to enumerate the keys and values in the dictionary and how to enumerate the keys and values alone using the xtd::collections::generic::dictionary::keys property and the xtd::collections::generic::dictionary::values property.
Finally, the example demonstrates the xtd::collections::generic::dictionary::remove method.
#include <xtd/xtd>
class example {
public:
static auto main() -> void {
open_with.add("txt", "notepad.exe");
open_with.add("bmp", "paint.exe");
open_with.add("dib", "paint.exe");
open_with.add("rtf", "wordpad.exe");
try {
open_with.add("txt", "winword.exe");
}
open_with["rtf"] = "winword.exe";
open_with["doc"] = "winword.exe";
try {
} catch (const key_not_found_exception&) {
}
auto value = ""_s;
if (open_with.try_get_value("tif", value))
else
if (!open_with.contains_key("ht")) {
open_with.add("ht", "hypertrm.exe");
}
for (const key_value_pair<string, string>& kvp : open_with)
dictionary<string, string>::value_collection values = open_with.values();
for(const auto& s : values)
dictionary<string, string>::key_collection
keys = open_with.keys();
for(const string& s : keys)
open_with.remove("doc");
if (!open_with.contains_key("doc"))
}
};
The exception that is thrown when one of the arguments provided to a method is not valid.
Definition argument_exception.hpp:23
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:284
- Note
- The speed of retrieval depends on the quality of the hashing algorithm of the type specified for key_t.
- Note
- For example, you can use the case-insensitive string comparers provided by the xtd::string_comparer class to create dictionaries with case-insensitive string keys.
|
| | dictionary () noexcept=default |
| | Initializes a new instance of the xtd::collections::generic::dictionary <key_t, value_t> class that is empty, has the default initial capacity, and uses the default equality comparer for the key type.
|
| | dictionary (const xtd::collections::generic::idictionary< key_t, value_t > &dictionary) |
| | Initializes a new instance of the xtd::collections::generic::dictionary <key_t, value_t> class that contains elements copied from the specified xtd::collections::generic::idictionary <key_t, value_t> and uses the default equality comparer for the key type.
|
| | dictionary (const xtd::collections::generic::ienumerable< value_type > &collection) |
| | Initializes a new instance of the xtd::collections::generic::dictionary <key_t, value_t> class that contains elements copied from the specified xtd::collections::generic::ienumerable <type_t>.
|
| | dictionary (const xtd::collections::generic::iequality_comparer< key_type > &comparer) |
| | Initializes a new instance of the xtd::collections::generic::dictionary <key_t, value_t> class that is empty, has the default initial capacity, and uses the specified xtd::collections::generic::iequality_comparer<type_t>.
|
| | dictionary (xtd::size capacity) |
| | Initializes a new instance of the xtd::collections::generic::dictionary <key_t, value_t> class that is empty, has the specified initial capacit.
|
| | dictionary (xtd::size capacity, const xtd::collections::generic::iequality_comparer< key_type > &comparer) |
| | Initializes a new instance of the xtd::collections::generic::dictionary <key_t, value_t> class that is empty, has the specified initial capacity, and uses the specified xtd::collections::generic::iequality_comparer <type_t>.
|
| | dictionary (const xtd::collections::generic::idictionary< key_t, value_t > &dictionary, const xtd::collections::generic::iequality_comparer< key_type > &comparer) |
| | Initializes a new instance of the xtd::collections::generic::dictionary <key_t, value_t> class that contains elements copied from the specified xtd::collections::generic::dictionary <key_t, value_t> and uses the specified xtd::collections::generic::iequality_comparer <type_t>.
|
| | dictionary (const xtd::collections::generic::ienumerable< value_type > &collection, const xtd::collections::generic::iequality_comparer< key_type > &comparer) |
| | Initializes a new instance of the xtd::collections::generic::dictionary <key_t, value_t> class that contains elements copied from the specified xtd::collections::generic::ienumerable <type_t> and uses the specified xtd::collections::generic::iequality_comparer <type_t>.
|
| | dictionary (dictionary &&other) noexcept=default |
| | Initializes instance of the xtd::collections::generic::dictionary <key_t, value_t> class from a variety of data sources. Optionally uses user supplied bucket_count as a minimal number of buckets to create, hash as the hash function, equal as the function to compare keys and alloc as the allocator.
|
| | dictionary (const dictionary &other) |
| | Initializes instance of the xtd::collections::generic::dictionary <key_t, value_t> class from a variety of data sources. Optionally uses user supplied bucket_count as a minimal number of buckets to create, hash as the hash function, equal as the function to compare keys and alloc as the allocator.
|
| | dictionary (std::unordered_map< key_t, value_t > &&other) |
| | Initializes instance of the xtd::collections::generic::dictionary <key_t, value_t> class from a variety of data sources. Optionally uses user supplied bucket_count as a minimal number of buckets to create, hash as the hash function, equal as the function to compare keys and alloc as the allocator.
|
| | dictionary (const std::unordered_map< key_t, value_t > &other) |
| | Initializes instance of the xtd::collections::generic::dictionary <key_t, value_t> class from a variety of data sources. Optionally uses user supplied bucket_count as a minimal number of buckets to create, hash as the hash function, equal as the function to compare keys and alloc as the allocator.
|
| | dictionary (std::initializer_list< base_value_type > init) |
| | Initializes instance of the xtd::collections::generic::dictionary <key_t, value_t> class from a variety of data sources.
|
| template<class init_key_t, class init_value_t> |
| | dictionary (std::initializer_list< key_value_pair< init_key_t, init_value_t > > init) |
| | Initializes instance of the xtd::collections::generic::dictionary <key_t, value_t> class from a variety of data sources.
|
| template<class input_iterator_t> |
| | dictionary (input_iterator_t first, input_iterator_t last) |
| | Initializes instance of the xtd::collections::generic::dictionary <key_t, value_t> class from a variety of data sources.
|
|
| auto | add (const key_t &key, const value_t &value) -> void override |
| | Adds an element with the provided key and value to the xtd::collections::generic::dictionary <key_t, value_t>.
|
| auto | add (const value_type &item) -> void override |
| | Adds an item to the xtd::collections::generic::icollection <type_t>.
|
| auto | clear () noexcept -> void override |
| | Removes all keys and values from the xtd::collections::generic::dictionary <key_t, value_t>.
|
| auto | contains (const value_type &item) const noexcept -> bool override |
| | Determines whether an element is in the xtd::collections::generic::dictionary <key_t, value_t>.
|
| auto | contains_key (const key_t &key) const noexcept -> bool override |
| | Determines whether the xtd::collections::generic::dictionary <key_t, value_t> contains the specified key.
|
| auto | contains_value (const value_t &value) const noexcept -> bool |
| | Determines whether the xtd::collections::generic::dictionary <key_t, value_t> contains the specified value.
|
| auto | copy_to (xtd::array< value_type > &array, xtd::size array_index) const -> void override |
| | Copies the elements of the xtd::collections::generic::icollection <type_t> to an xtd::array, starting at a particular xtd::array index.
|
| auto | ensure_capacity (xtd::size capacity) noexcept -> size_type |
| | Ensures that the dictionary can hold up to a specified number of entries without any further expansion of its backing storage.
|
| enumerator< value_type > | get_enumerator () const noexcept override |
| | Returns an enumerator that iterates through the xtd::collections::generic::dictionary <key_t, value_t>.
|
| auto | remove (const key_t &key) noexcept -> bool override |
| | Removes the value with the specified key from the xtd::collections::generic::dictionary <key_t, value_t>.
|
| auto | remove (const value_type &item) noexcept -> bool override |
| | Removes the first occurrence of a specific object from the xtd::collections::generic::dictionary <key_t, value_t>.
|
| auto | remove (const key_t &key, value_t &value) noexcept -> bool |
| | Removes the value with the specified key from the xtd::collections::generic::dictionary <key_t, value_t>, and copies the element to the value parameter.
|
| auto | to_string () const noexcept -> xtd::string override |
| | Gets a string that represents the current object.
|
| auto | trim_excess (size_type capacity) -> void |
| | Sets the capacity of this dictionary to hold up a specified number of entries without any further expansion of its backing storage.
|
| auto | trim_excess () -> void |
| | Sets the capacity of this dictionary to what it would be if it had been originally initialized with all its entries.
|
| auto | try_add (const key_t &key, const value_t value) noexcept -> bool |
| | Attempts to add the specified key and value to the dictionary.
|
| auto | try_get_value (const key_t &key, value_t &value) const -> bool override |
| | Gets the value associated with the specified key.
|
|
| auto | operator= (dictionary &&other) noexcept -> dictionary & |
| | Move assignment operator. Replaces the contents with a copy of the contents of other.
|
| auto | operator= (std::unordered_map< key_t, value_t > &&other) noexcept -> dictionary & |
| | Move assignment operator. Replaces the contents with a copy of the contents of other.
|
| auto | operator= (const dictionary &other) noexcept -> dictionary &=default |
| | Copy assignment operator. Replaces the contents with a copy of the contents of other.
|
| auto | operator= (const std::unordered_map< key_t, value_t > &other) noexcept -> dictionary & |
| | Copy assignment operator. Replaces the contents with a copy of the contents of other.
|
| auto | operator= (std::initializer_list< base_value_type > ilist) -> dictionary & |
| | Copy assignment operator. Replaces the contents with a copy of the contents of other.
|
| template<class init_key_t, class init_value_t> |
| auto | operator= (std::initializer_list< key_value_pair< init_key_t, init_value_t > > ilist) -> dictionary & |
| | Copy assignment operator. Replaces the contents with a copy of the contents of other.
|
| const value_t & | operator[] (const key_t &key) const override |
| | Gets the element with the specified key.
|
| value_t & | operator[] (const key_t &key) override |
| | Sets the element with the specified key.
|
| | operator const base_type & () const noexcept |
| | Returns a reference to the underlying base type.
|
| | operator base_type & () noexcept |
| | Returns a reference to the underlying base type.
|
| | operator std::unordered_map< key_t, value_t > () const noexcept |
| | Gets a std::unordered_map<key_t, value_t>.
|
|
| using | key_type |
| | Represents the dictionary key type.
|
| using | mapped_type |
| | Represents the dictionary mapped type.
|
| using | value_type |
| | Represents the xtd::collections::generic::idictionary value type.
|
| using | iterator |
| | Represents the iterator of xtd::collections::generic::ienumerable value type.
|
| using | const_iterator |
| | Represents the const iterator of xtd::collections::generic::ienumerable value type.
|
| using | key_collection |
| | Represents the idictionary key collection type.
|
| using | value_collection |
| | Represents the idictionary value collection type.
|
| | object ()=default |
| | Create a new instance of the ultimate base class object.
|
| virtual bool | equals (const object &obj) const noexcept |
| | Determines whether the specified object is equal to the current object.
|
| virtual xtd::size | get_hash_code () const noexcept |
| | Serves as a hash function for a particular type.
|
| virtual type_object | get_type () const noexcept |
| | Gets the type of the current instance.
|
| template<class object_t> |
| xtd::unique_ptr_object< object_t > | memberwise_clone () const |
| | Creates a shallow copy of the current object.
|
| template<class object_a_t, class object_b_t> |
| static bool | equals (const object_a_t &object_a, const object_b_t &object_b) noexcept |
| | Determines whether the specified object instances are considered equal.
|
| template<class object_a_t, class object_b_t> |
| static bool | reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept |
| | Determines whether the specified object instances are the same instance.
|