std::multimap<Key,T,Compare,Allocator>:: swap
|
void
swap
(
multimap
&
other
)
;
|
(C++17まで) | |
|
void
swap
(
multimap
&
other
)
noexcept
(
/* see below */
)
;
|
(C++17から)
(constexprはC++26から) |
|
コンテナの内容を other の内容と交換します。個々の要素に対するムーブ、コピー、スワップ操作は一切呼び出しません。
すべてのイテレータと参照は有効なままです。
end()
イテレータは無効化されます。
Compare
は
Swappable
でなければならず、この型のオブジェクトは非メンバ
swap
への修飾なし呼び出しを使用して交換されます。
|
std::
allocator_traits
<
allocator_type
>
::
propagate_on_container_swap
::
value
が
true
の場合、アロケータは非メンバー関数
|
(C++11以降) |
目次 |
パラメータ
| other | - | 内容を交換するコンテナ |
例外
|
|
(C++17まで) |
|
noexcept
指定:
noexcept
(
std::
allocator_traits
<
Allocator
>
::
is_always_equal
::
value
&& std:: is_nothrow_swappable < Compare > :: value ) |
(C++17以降) |
計算量
定数。
例
#include <iostream> #include <string> #include <utility> #include <map> // 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; } // コンテナを出力 template<class Os, class Co> Os& operator<<(Os& os, const Co& co) { os << '{'; for (const auto& i : co) os << ' ' << i; return os << " }\n"; } int main() { std::multimap<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'; // スワップ前に一方のコンテナ内の要素を参照していたすべてのイテレータは、 // スワップ後には他方のコンテナ内の同じ要素を参照することに注意。 // 参照についても同様。 }
出力:
──────── before swap ────────
m1: { α:alpha β:beta γ:gamma γ:gamma }
m2: { δ:delta ε:epsilon ε:epsilon }
ref: α:alpha
iter: β:beta
──────── after swap ────────
m1: { δ:delta ε:epsilon ε:epsilon }
m2: { α:alpha β:beta γ:gamma γ:gamma }
ref: α:alpha
iter: β:beta
関連項目
|
std::swap
アルゴリズムを特殊化する
(関数テンプレート) |