最終更新日時(UTC): 2026年02月17日 14時22分33秒
Akira Takahashi が更新

履歴 編集

function
<stack>

std::stack::swap(C++11)

void swap(stack& s) noexcept(noexcept(swap(c, s.c)));           // (1) C++11
constexpr void swap(stack& s) noexcept(noexcept(swap(c, s.c))); // (1) C++26

概要

他の stack オブジェクトと値を入れ替える。

効果

swap(c, s.c)

戻り値

なし

例外

swap(c, s.c)例外を投げない場合、この関数は決して例外を投げない。

#include <iostream>
#include <stack>

int main()
{
  std::stack<int> x;
  x.push(3);
  x.push(1);
  x.push(4);

  std::stack<int> y;
  y.push(2);
  y.push(7);
  y.push(1);

  // 交換
  x.swap(y);

  // それぞれの要素を表示
  std::cout << "x : ";
  while (!x.empty()) {
    std::cout << x.top() << " ";
    x.pop();
  }

  std::cout << std::endl;

  std::cout << "y : ";
  while (!y.empty()) {
    std::cout << y.top() << " ";
    y.pop();
  }
}

出力

x : 1 7 2 
y : 4 1 3 

実装例

void swap(stack& s) noexcept(noexcept(swap(c, s.c)))
{
  using std::swap;
  swap(c, s.c);
}

バージョン

言語

  • C++11

処理系

参照

Morty Proxy This is a proxified and sanitized view of the page, visit original site.