std:: swap (std::stack)
|
헤더 파일에 정의됨
<stack>
|
||
|
template
<
class
T,
class
Container
>
void
swap
(
std::
stack
<
T, Container
>
&
lhs,
|
(C++11부터)
(C++17까지) |
|
|
template
<
class
T,
class
Container
>
void
swap
(
std::
stack
<
T, Container
>
&
lhs,
|
(C++17부터)
(C++26부터 constexpr) |
|
|
이 오버로드는 std:: is_swappable_v < Container > 가 true 인 경우에만 오버로드 해결에 참여합니다. |
(since C++17) |
목차 |
매개변수
| lhs, rhs | - | 내용을 교환할 컨테이너들 |
복잡도
기본 컨테이너를 교환하는 것과 동일합니다.
예외
|
noexcept
명세:
noexcept
(
noexcept
(
lhs.
swap
(
rhs
)
)
)
|
(C++17 이후) |
참고 사항
컨테이너 어댑터에 대한 std::swap 오버로드가 C++11에서 도입되었지만, 컨테이너 어댑터는 C++98에서 이미 std::swap 으로 교환될 수 있었습니다. 이러한 std::swap 호출은 일반적으로 선형 시간 복잡도를 가지지만, 더 나은 복잡도가 제공될 수 있습니다.
예제
#include <algorithm> #include <iostream> #include <stack> int main() { std::stack<int> alice; std::stack<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); // 스왑 전 상태 출력 print("Alice:", alice); print("Bobby:", bob); std::cout << "-- SWAP\n"; std::swap(alice, bob); // 스왑 후 상태 출력 print("Alice:", alice); print("Bobby:", bob); }
출력:
Alice: size=3 top=3 Bobby: size=4 top=10 -- SWAP Alice: size=4 top=10 Bobby: size=3 top=3
참고 항목
|
(C++11)
|
내용을 교환합니다
(public member function) |