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

履歴 編集

function template
<forward_list>

std::swap (非メンバ関数)(C++11)

namespace std {
  template <class T, class Allocator>
  void swap(forward_list<T, Allocator>& x,
            forward_list<T, Allocator>& y);  // (1) C++11

  template <class T, class Allocator>
  void swap(forward_list<T,Allocator>& x,
            forward_list<T,Allocator>& y)
              noexcept(noexcept(x.swap(y))); // (1) C++17
}

概要

2つのforward_listオブジェクトを入れ替える。

効果

x.swap(y)

戻り値

なし

#include <iostream>
#include <forward_list>
#include <string>
#include <algorithm> // std::for_each

template <class T>
void print(const std::string& name, const std::forward_list<T>& ls)
{
  std::cout << name << " : {";
  std::for_each(ls.begin(), ls.end(), [](const T& x) { std::cout << x << " "; });
  std::cout << "}" << std::endl;
}

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

  std::swap(ls1, ls2);

  print("ls1", ls1);
  print("ls2", ls2);
}

出力

ls1 : {1 2 3 }
ls2 : {4 5 6 }

バージョン

言語

  • C++11

処理系

参照