std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>:: insert_or_assign
From cppreference.net
|
template
<
class
M
>
std:: pair < iterator, bool > insert_or_assign ( const key_type & k, M && obj ) ; |
(1) | (seit C++23) |
|
template
<
class
M
>
std:: pair < iterator, bool > insert_or_assign ( key_type && k, M && obj ) ; |
(2) | (seit C++23) |
|
template
<
class
K,
class
M
>
std:: pair < iterator, bool > insert_or_assign ( K && k, M && obj ) ; |
(3) | (seit C++23) |
|
template
<
class
M
>
iterator insert_or_assign ( const_iterator hint, const key_type & k, M && obj ) ; |
(4) | (seit C++23) |
|
template
<
class
M
>
iterator insert_or_assign ( const_iterator hint, key_type && k, M && obj ) ; |
(5) | (seit C++23) |
|
template
<
class
K,
class
M
>
iterator insert_or_assign ( const_iterator hint, K && k, M && obj ) ; |
(6) | (seit C++23) |
1,2)
Wenn ein Schlüsseläquivalent zu
k
bereits im Container existiert, weist
std::
forward
<
M
>
(
obj
)
dem
mapped_type
zu, das dem Schlüssel
k
entspricht. Wenn der Schlüssel nicht existiert, fügt den neuen Wert ein wie durch
- (1,2) try_emplace ( std:: forward < decltype ( k ) > ( k ) , std:: forward < M > ( obj ) ) ,
- (4,5) try_emplace ( hint, std:: forward < decltype ( k ) > ( k ) , std:: forward < M > ( obj ) ) .
Das Programm ist fehlerhaft, falls entweder
std::
is_assignable_v
<
mapped_type
&
, M
>
oder
std::
is_constructible_v
<
mapped_type, M
>
gleich
false
ist.
3,6)
Falls ein Schlüsseläquivalent zu
k
bereits im Container existiert, weist
std::
forward
<
M
>
(
obj
)
dem
mapped_type
entsprechend dem Schlüssel
k
zu. Andernfalls entspricht es
- (3) try_emplace ( std:: forward < K > ( k ) , std:: forward < M > ( obj ) ) ,
- (6) try_emplace ( hint, std:: forward < K > ( k ) , std:: forward < M > ( obj ) ) .
Die Konvertierung von
k
in
key_type
muss ein Objekt
u
konstruieren, für das
find
(
k
)
==
find
(
u
)
true
ist. Andernfalls ist das Verhalten undefiniert.
Diese Überladungen nehmen nur dann an der Überladungsauflösung teil, wenn:
-
Der qualifizierte Bezeichner
Compare::is_transparentgültig ist und einen Typ bezeichnet. - std:: is_constructible_v < key_type, K > ist true .
- std:: is_assignable_v < mapped_type & , M > ist true .
- std:: is_constructible_v < mapped_type, M > ist true .
| Informationen zur Iterator-Invalidierung sind kopiert von hier |
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
1-3)
Die
bool
-Komponente ist
true
, wenn die Einfügung stattgefunden hat, und
false
, wenn die Zuweisung stattgefunden hat. Die Iterator-Komponente zeigt auf das Element, das eingefügt oder aktualisiert wurde.
4-6)
Iterator, der auf das eingefügte oder aktualisierte Element zeigt.
Komplexität
1-3)
Gleich wie für
emplace
.
4-6)
Gleich wie für
emplace_hint
.
Hinweise
insert_or_assign
liefert mehr Informationen als
operator
[
]
und erfordert keine Default-Konstruierbarkeit des mapped type.
Beispiel
Diesen Code ausführen
#include <flat_map> #include <iostream> #include <string> 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::flat_map<std::string, std::string> map; print_result(map.insert_or_assign("a", "apple")); print_result(map.insert_or_assign("b", "banana")); print_result(map.insert_or_assign("c", "cherry")); print_result(map.insert_or_assign("c", "clementine")); for (const auto& node : map) print_node(node); }
Ausgabe:
inserted: [a] = apple inserted: [b] = banana inserted: [c] = cherry assigned: [c] = clementine [a] = apple [b] = banana [c] = clementine
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
(öffentliche Elementfunktion) |
|
|
konstruiert Elemente direkt vor Ort
(öffentliche Elementfunktion) |
|
|
fügt direkt vor Ort ein, wenn der Schlüssel nicht existiert, tut nichts, wenn der Schlüssel existiert
(öffentliche Elementfunktion) |