std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: insert_or_assign
|
template
<
class
M
>
std:: pair < iterator, bool > insert_or_assign ( const Key & k, M && obj ) ; |
(1) | (seit C++17) |
|
template
<
class
M
>
std:: pair < iterator, bool > insert_or_assign ( Key && k, M && obj ) ; |
(2) | (seit C++17) |
|
template
<
class
K,
class
M
>
std:: pair < iterator, bool > insert_or_assign ( K && k, M && obj ) ; |
(3) | (seit C++26) |
|
template
<
class
M
>
iterator insert_or_assign ( const_iterator hint, const Key & k, M && obj ) ; |
(4) | (seit C++17) |
|
template
<
class
M
>
iterator insert_or_assign ( const_iterator hint, Key && k, M && obj ) ; |
(5) | (seit C++17) |
|
template
<
class
K,
class
M
>
iterator insert_or_assign ( const_iterator hint, K && k, M && obj ) ; |
(6) | (seit C++26) |
mapped_type
zu, das dem Schlüssel
k
entspricht. Falls der Schlüssel nicht existiert, fügt den neuen Wert ein, als ob durch
insert
, und konstruiert ihn aus
value_type
(
k,
std::
forward
<
M
>
(
obj
)
)
.
mapped_type
des entsprechenden Schlüssels zugewiesen. Falls der Schlüssel nicht existiert, wird ein Objekt
u
vom Typ
value_type
mit
std::
forward
<
K
>
(
k
)
,
std::
forward
<
M
>
(
obj
)
)
konstruiert und anschließend
u
in
*
this
eingefügt. Falls
hash_function
(
)
(
u.
first
)
!
=
hash_function
(
)
(
k
)
||
contains
(
u.
first
)
gleich
true
ist, ist das Verhalten undefiniert. Der
value_type
muss
EmplaceConstructible
in
unordered_map
aus
std::
forward
<
K
>
(
k
)
,
std::
forward
<
M
>
(
obj
)
sein. Diese Überladung nimmt nur an der Überladungsauflösung teil, wenn
Hash
und
KeyEqual
beide
transparent
sind. Dies setzt voraus, dass ein solcher
Hash
sowohl mit dem Typ
K
als auch mit dem Typ
Key
aufrufbar ist und dass
KeyEqual
transparent ist, was zusammen den Aufruf dieser Funktion ermöglicht, ohne eine Instanz von
Key
zu konstruieren.
Das Verhalten ist undefiniert (bis C++20) Das Programm ist fehlerhaft (seit C++20) falls std:: is_assignable_v < mapped_type & , M && > false ist.
Falls nach der Operation die neue Anzahl der Elemente größer ist als das alte
max_load_factor()
*
bucket_count()
findet eine Neuverteilung statt.
Falls eine Neuverteilung auftritt (aufgrund der Einfügung), werden alle Iteratoren ungültig. Andernfalls (keine Neuverteilung) werden Iteratoren nicht ungültig.
Inhaltsverzeichnis |
Parameter
| k | - | der Schlüssel, der sowohl für die Suche als auch für das Einfügen verwendet wird, falls nicht gefunden |
| hint | - | Iterator auf die Position, vor der das neue Element eingefügt wird |
| obj | - | der einzufügende oder zuzuweisende Wert |
Rückgabewert
Komplexität
emplace
.
emplace_hint
.
Hinweise
insert_or_assign
liefert mehr Informationen als
operator
[
]
und erfordert keine Default-Konstruierbarkeit des mapped type.
| Feature-Test Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_unordered_map_try_emplace
|
201411L
|
(C++17) |
std::unordered_map::try_emplace
,
std::unordered_map::insert_or_assign
|
__cpp_lib_associative_heterogeneous_insertion
|
202311L
|
(C++26) | Heterogene Überladungen für die verbleibenden Member-Funktionen in geordneten und ungeordneten assoziativen Containern . Überladungen ( 3 ) und ( 6 ) . |
Beispiel
#include <iostream> #include <string> #include <unordered_map> void print_node(const auto& node) { std::cout << '[' << node.first << "] = " << node.second << '\n'; } void print_result(auto const& pair) { std::cout << (pair.second ? "inserted: " : "assigned: "); print_node(*pair.first); } int main() { std::unordered_map<std::string, std::string> myMap; print_result(myMap.insert_or_assign("a", "apple")); print_result(myMap.insert_or_assign("b", "banana")); print_result(myMap.insert_or_assign("c", "cherry")); print_result(myMap.insert_or_assign("c", "clementine")); for (const auto& node : myMap) print_node(node); }
Mögliche Ausgabe:
inserted: [a] = apple inserted: [b] = banana inserted: [c] = cherry assigned: [c] = clementine [c] = clementine [a] = apple [b] = banana
Siehe auch
|
greift auf ein bestimmtes Element zu oder fügt es ein
(öffentliche Elementfunktion) |
|
|
greift auf ein bestimmtes Element mit Bereichsprüfung zu
(öffentliche Elementfunktion) |
|
|
fügt Elemente ein
oder Knoten
(seit C++17)
(öffentliche Elementfunktion) |
|
|
konstruiert Elemente direkt vor Ort
(öffentliche Elementfunktion) |