std::unordered_map
: cppreference.com
<tbody>
</tbody>
template< class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class Allocator = std::allocator< std::pair<const Key, T> > > class unordered_map; |
(1) | (C++11) |
namespace pmr { template <class Key, class T, class Hash = std::hash<Key>, class Pred = std::equal_to<Key>> using unordered_map = std::unordered_map<Key, T, Hash, Pred, std::pmr::polymorphic_allocator<std::pair<const Key,T>>>; } |
(2) | (C++17) |
unordered map
std::unordered_map Container, AllocatorAwareContainer, UnorderedAssociativeContainer
| swap, std::swap | |
| clear, rehash, reserve, operator= | |
| insert, emplace, emplace_hint, operator[] | |
| erase |
- swap swap
key_type
|
Key
|
mapped_type
|
T
|
value_type
|
std::pair<const Key, T>
|
size_type
|
( std::size_t) |
difference_type
|
( std::ptrdiff_t) |
hasher
|
Hash
|
key_equal
|
KeyEqual
|
allocator_type
|
Allocator
|
reference
|
value_type&
|
const_reference
|
const value_type&
|
pointer
|
std::allocator_traits<Allocator>::pointer
|
const_pointer
|
std::allocator_traits<Allocator>::const_pointer
|
iterator
|
LegacyForwardIterator |
const_iterator
|
const LegacyForwardIterator |
local_iterator
|
value, difference, pointer, reference iterator
|
const_local_iterator
|
value, difference, pointer, reference const_iterator
|
node_type(C++17) |
|
insert_return_type(C++17) |
node_type iterator node_type template <class Iter, class NodeType> struct /*unspecified*/ {
Iter position;
bool inserted;
NodeType node;
};
|
unordered_map () | |
unordered_map () | |
| () | |
| () | |
| () | |
| () | |
| () | |
| () | |
| () | |
| () | |
| (C++17) () | |
(C++17) |
() |
| () | |
| () | |
(C++17) |
() |
| () | |
(C++11) |
() |
(C++17) |
() |
(C++17) |
() |
| () | |
| () | |
| () | |
| () | |
(C++20) |
() |
| () | |
| () | |
| () | |
| () | |
| () | |
| () | |
| () | |
| () | |
| () | |
| () | |
| () | |
| () | |
| () | |
(C++20) |
unordered_map () |
| std::swap () | |
(C++20) |
() |
(C++17)
Run this code
#include <iostream>
#include <string>
#include <unordered_map>
int main()
{
// Create an unordered_map of three strings (that map to strings)
std::unordered_map<std::string, std::string> u = {
{"RED","#FF0000"},
{"GREEN","#00FF00"},
{"BLUE","#0000FF"}
};
// Iterate and print keys and values of unordered_map
for( const auto& n : u ) {
std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n";
}
// Add two new entries to the unordered_map
u["BLACK"] = "#000000";
u["WHITE"] = "#FFFFFF";
// Output values by key
std::cout << "The HEX of color RED is:[" << u["RED"] << "]\n";
std::cout << "The HEX of color BLACK is:[" << u["BLACK"] << "]\n";
return 0;
}
:
Key:[RED] Value:[#FF0000]
Key:[BLUE] Value:[#0000FF]
Key:[GREEN] Value:[#00FF00]
The HEX of color RED is:[#FF0000]
The HEX of color BLACK is:[#000000]