std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::at
Von cppreference.com
| T& at( const Key& key ); |
(1) | (seit C++23) |
| const T& at( const Key& key ) const; |
(2) | (seit C++23) |
template< class K > T& at( const K& x ); |
(3) | (seit C++23) |
| template< class K > const T& at( const K& x ) const; |
(4) | (seit C++23) |
Gibt eine Referenz auf den zugeordneten Wert des Elements mit dem angegebenen Schlüssel zurück. Wenn kein solches Element existiert, wird eine Ausnahme vom Typ std::out_of_range ausgelöst.
1,2) Der Schlüssel ist äquivalent zu key.
3,4) Der Schlüssel ist *äquivalent* zu dem Wert x. Die Referenz auf den zugeordneten Wert wird erhalten, als ob durch den Ausdruck this->find(x)->second.
Der Ausdruck this->find(x) muss wohlgeformt sein und ein wohldefiniertes Verhalten haben, andernfalls ist das Verhalten undefiniert.
Diese Überladungen nehmen nur an der Überladungsauflösung teil, wenn die qualifizierte ID Compare::is_transparent gültig ist und einen Typ bezeichnet. Sie ermöglicht das Aufrufen dieser Funktion, ohne eine Instanz von
Key zu konstruieren.Inhalt |
[edit] Parameter
| key | - | der Schlüssel des zu findenden Elements |
| x | - | ein Wert eines beliebigen Typs, der transparent mit einem Schlüssel verglichen werden kann |
[edit] Rückgabewert
Eine Referenz auf den zugeordneten Wert des angeforderten Elements.
[edit] Ausnahmen
3,4) std::out_of_range, wenn der Container das angegebene Element nicht hat, d.h. wenn find(x) == end() true ist.
[edit] Komplexität
Logarithmisch zur Größe des Containers.
[edit] Beispiel
Führen Sie diesen Code aus
#include <cassert> #include <iostream> #include <flat_map> struct LightKey { int o; }; struct HeavyKey { int o[1000]; }; // The container must use std::less<> (or other transparent Comparator) to // access overloads (3,4). This includes standard overloads, such as // comparison between std::string and std::string_view. bool operator<(const HeavyKey& x, const LightKey& y) { return x.o[0] < y.o; } bool operator<(const LightKey& x, const HeavyKey& y) { return x.o < y.o[0]; } bool operator<(const HeavyKey& x, const HeavyKey& y) { return x.o[0] < y.o[0]; } int main() { std::flat_map<int, char> map{{1, 'a'}, {2, 'b'}}; assert(map.at(1) == 'a'); assert(map.at(2) == 'b'); try { map.at(13); } catch(const std::out_of_range& ex) { std::cout << "1) out_of_range::what(): " << ex.what() << '\n'; } #ifdef __cpp_lib_associative_heterogeneous_insertion // Transparent comparison demo. std::flat_map<HeavyKey, char, std::less<>> map2{{{1}, 'a'}, {{2}, 'b'}}; assert(map2.at(LightKey{1}) == 'a'); assert(map2.at(LightKey{2}) == 'b'); try { map2.at(LightKey{13}); } catch(const std::out_of_range& ex) { std::cout << "2) out_of_range::what(): " << ex.what() << '\n'; } #endif }
Mögliche Ausgabe
1) out_of_range::what(): map::at: key not found 2) out_of_range::what(): map::at: key not found
[edit] Siehe auch
| greift auf ein Element zu oder fügt es ein (public member function) | |
| sucht ein Element mit einem bestimmten Schlüssel (public member function) |