std::priority_queue<T,Container,Compare>::swap
來自 cppreference.com
void swap( priority_queue& other ) noexcept(/* 見下文 */); |
(C++11 起) | |
other
的內容。實際呼叫using std::swap; swap(c, other.c); swap(comp, other.comp);
目錄 |
[編輯] 引數
其他 | - | 用於交換內容的容器介面卡 |
[編輯] 返回值
(無)
[編輯] 異常
noexcept 規範:
noexcept(noexcept(swap(c, other.c)) && noexcept(swap(comp, other.comp))) 在上述表示式中,識別符號 |
(C++11 起) (C++17 前) |
noexcept 規範:
noexcept(std::is_nothrow_swappable_v<Container> && std::is_nothrow_swappable_v<Compare>) |
(C++17 起) |
[編輯] 複雜度
與底層容器相同(通常是常數時間)。
注意
一些實現(例如 libc++)提供 `swap` 成員函式作為 C++11 之前模式的擴充套件。
[編輯] 示例
執行此程式碼
#include <iostream> #include <concepts> #include <functional> #include <queue> #include <string> #include <string_view> #include <vector> template<typename Adaptor> requires (std::ranges::input_range<typename Adaptor::container_type>) void print(std::string_view name, const Adaptor& adaptor) { struct Printer : Adaptor // to use protected Adaptor::Container c; { void print(std::string_view name) const { std::cout << name << " [" << std::size(this->c) << "]: "; for (auto const& elem : this->c) std::cout << elem << ' '; std::cout << '\n'; } }; static_cast<Printer const&>(adaptor).print(name); } int main() { std::vector<std::string> v1{"1","2","3","4"}, v2{"Ɐ","B","Ɔ","D","Ǝ"}; std::priority_queue s1(std::less<>(), std::move(v1)); std::priority_queue s2(std::less<>(), std::move(v2)); print("s1", s1); print("s2", s2); s1.swap(s2); print("s1", s1); print("s2", s2); }
輸出
s1 [4]: 4 3 2 1 s2 [5]: Ǝ D Ɔ B Ɐ s1 [5]: Ǝ D Ɔ B Ɐ s2 [4]: 4 3 2 1
缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2456 | C++11 | noexcept 規範格式錯誤 |
已修復 |
[編輯] 參閱
特化 std::swap 演算法 (函式模板) |