stack_swap

stack_swap (std::stack::swap — 내용 교환)

std::stack 하나의 내용을 다른 객체 other와 맞바꾸는 멤버 함수예요.

출처: cppreference

본문

시그니처는 다음과 같아요.

void swap( stack& other ) noexcept(/* see below */);   // (since C++11)

컨테이너 어댑터의 내용을 other의 내용과 교환해요. 내부적으로 c.swap(other.c)를 호출해요.

매개변수

  • other: 내용을 교환할 컨테이너 어댑터

복잡도

Container::swap의 복잡도와 같아요.

예제

#include <iostream>
#include <stack>
int main()
{
    std::stack<int> a, b;
    a.push(1); b.push(2);
    a.swap(b);
    std::cout << a.top() << '\n';   // 2
}

더 알아보기 (Learn more)

cppreference