std::multimap<Key,T,Compare,Allocator>:: swap
|
void
swap
(
multimap
&
other
)
;
|
(C++17 이전) | |
|
void
swap
(
multimap
&
other
)
noexcept
(
/* see below */
)
;
|
(C++17 이후)
(C++26 이후 constexpr) |
|
컨테이너의 내용을 other 와 교환합니다. 개별 요소에 대한 이동, 복사 또는 스왑 연산을 호출하지 않습니다.
모든 반복자와 참조는 유효하게 유지됩니다.
end()
반복자는 무효화됩니다.
Compare
는
Swappable
해야 하며, 이 타입의 객체들은 비멤버
swap
함수에 대한 한정자 없는 호출을 사용하여 교환됩니다.
|
만약
std::
allocator_traits
<
allocator_type
>
::
propagate_on_container_swap
::
value
가
true
인 경우, 할당자는 비멤버
|
(C++11부터) |
목차 |
매개변수
| other | - | 내용을 교환할 대상 컨테이너 |
예외
|
|
(until C++17) |
|
noexcept
명세:
noexcept
(
std::
allocator_traits
<
Allocator
>
::
is_always_equal
::
value
&& std:: is_nothrow_swappable < Compare > :: value ) |
(since 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 << "──────── swap 전 ────────\n" << "m1: " << m1 << "m2: " << m2 << "ref: " << ref << "\niter: " << *iter << '\n'; m1.swap(m2); std::cout << "──────── swap 후 ────────\n" << "m1: " << m1 << "m2: " << m2 << "ref: " << ref << "\niter: " << *iter << '\n'; // swap 전 각 컨테이너의 요소를 참조하는 모든 반복자는 // swap 후 다른 컨테이너의 동일한 요소를 참조함 // 참조자도 마찬가지 }
출력:
──────── swap 전 ────────
m1: { α:alpha β:beta γ:gamma γ:gamma }
m2: { δ:delta ε:epsilon ε:epsilon }
ref: α:alpha
iter: β:beta
──────── swap 후 ────────
m1: { δ:delta ε:epsilon ε:epsilon }
m2: { α:alpha β:beta γ:gamma γ:gamma }
ref: α:alpha
iter: β:beta
참고 항목
|
std::swap
알고리즘을 특수화함
(함수 템플릿) |