最終更新日時(UTC):
が更新

履歴 編集

function
<forward_list>

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

void swap(forward_list& x); // (1) C++11

void swap(forward_list& x)
       noexcept(
         allocator_traits<Allocator>::is_always_equal::value
       );                   // (1) C++17

概要

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

効果

*thisの内容をxと交換する。

戻り値

なし

計算量

定数時間

#include <iostream>
#include <forward_list>
#include <algorithm>

int main()
{
  std::forward_list<int> ls1 = {1, 2, 3};
  std::forward_list<int> ls2 = {4, 5, 6};

  ls1.swap(ls2);

  std::for_each(ls1.begin(), ls1.end(), [](int x) {
    std::cout << x << std::endl;
  });

  std::cout << std::endl;

  std::for_each(ls2.begin(), ls2.end(), [](int x) {
    std::cout << x << std::endl;
  });
}

出力

4
5
6

1
2
3

バージョン

言語

  • C++11

処理系

参照