std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::swap
From cppreference.com
void swap( unordered_map& other ); |
(C++11以降) (C++17まで) |
|
| void swap( unordered_map& other ) noexcept(/* 下記参照 */); |
(C++17以降) | |
コンテナの内容を other の内容と交換します。個々の要素に対するムーブ、コピー、またはスワップ操作は呼び出されません。
すべてのイテレータと参照は有効なままです。end() イテレータは無効になります。Hash および KeyEqual オブジェクトは Swappable である必要があり、これらは非メンバ swap の修飾されていない呼び出しを使用して交換されます。
|
std::allocator_traits<allocator_type>::propagate_on_container_swap::value が true の場合、アロケータは非メンバ |
(C++11以降) |
目次 |
[編集] パラメータ
| その他 | - | 内容を交換するコンテナ |
[編集] 例外
|
|
(C++17まで) |
|
noexcept 指定:
noexcept(std::allocator_traits<Allocator>::is_always_equal::value && std::is_nothrow_swappable<Hash>::value |
(C++17以降) |
[編集] 計算量
定数。
[編集] 例
このコードを実行
#include <iostream> #include <string> #include <utility> #include <unordered_map> // print out a std::pair template<class Os, class U, class V> Os& operator<<(Os& os, const std::pair<U, V>& p) { return os << p.first << ':' << p.second; } // print out a container template<class Os, class Co> Os& operator<<(Os& os, const Co& co) { os << '{'; for (auto const& i : co) os << ' ' << i; return os << " }\n"; } int main() { std::unordered_map<std::string, std::string> m1{{"γ", "gamma"}, {"β", "beta"}, {"α", "alpha"}, {"γ", "gamma"}}, m2{{"ε", "epsilon"}, {"δ", "delta"}, {"ε", "epsilon"}}; const auto& ref = *(m1.begin()); const auto iter = std::next(m1.cbegin()); std::cout << "──────── before swap ────────\n" << "m1: " << m1 << "m2: " << m2 << "ref: " << ref << "\niter: " << *iter << '\n'; m1.swap(m2); std::cout << "──────── after swap ────────\n" << "m1: " << m1 << "m2: " << m2 << "ref: " << ref << "\niter: " << *iter << '\n'; // Note that every iterator referring to an element in one container before // the swap refers to the same element in the other container after the swap. // Same is true for references. }
実行結果の例
──────── before swap ────────
m1: { α:alpha β:beta γ:gamma }
m2: { δ:delta ε:epsilon }
ref: α:alpha
iter: β:beta
──────── after swap ────────
m1: { δ:delta ε:epsilon }
m2: { α:alpha β:beta γ:gamma }
ref: α:alpha
iter: β:beta[編集] 関連項目
| std::swap アルゴリズムを特殊化する (関数テンプレート) |