std:: swap (std::priority_queue)
|
Definiert in Header
<queue>
|
||
|
template
<
class
T,
class
Container,
class
Compare
>
void
swap
(
std::
priority_queue
<
T, Container, Compare
>
&
lhs,
|
(seit C++11)
(bis C++17) |
|
|
template
<
class
T,
class
Container,
class
Compare
>
void
swap
(
std::
priority_queue
<
T, Container, Compare
>
&
lhs,
|
(seit C++17)
(constexpr seit C++26) |
|
|
Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn std:: is_swappable_v < Container > und std:: is_swappable_v < Compare > beide true sind. |
(seit C++17) |
InhaltsverzeichnisParameter
KomplexitätGleichbedeutend mit dem Austausch der zugrundeliegenden Container. Ausnahmen
HinweiseObwohl die Überladungen von std::swap für Container-Adapter in C++11 eingeführt wurden, konnten Container-Adapter bereits in C++98 durch std::swap getauscht werden. Solche Aufrufe von std::swap haben normalerweise lineare Zeitkomplexität, aber eine bessere Komplexität kann bereitgestellt werden. Beispiel
Diesen Code ausführen
#include <algorithm> #include <iostream> #include <queue> int main() { std::priority_queue<int> alice; std::priority_queue<int> bob; auto print = [](const auto& title, const auto& cont) { std::cout << title << " size=" << cont.size(); std::cout << " top=" << cont.top() << '\n'; }; for (int i = 1; i < 4; ++i) alice.push(i); for (int i = 7; i < 11; ++i) bob.push(i); // Zustand vor Swap ausgeben print("Alice:", alice); print("Bobby:", bob); std::cout << "-- SWAP\n"; std::swap(alice, bob); // Zustand nach Swap ausgeben print("Alice:", alice); print("Bobby:", bob); } Ausgabe: Alice: size=3 top=3 Bobby: size=4 top=10 -- SWAP Alice: size=4 top=10 Bobby: size=3 top=3 Siehe auch
|