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

履歴 編集

function template
<vector>

std::swap (非メンバ関数)

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

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

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

概要

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

効果

x.swap(y)

戻り値

なし

#include <iostream>
#include <vector>

template <class T>
void print(const char* name, const std::vector<T>& v)
{
  std::cout << name << " : {";

  for (const T& x : v) {
    std::cout << x << " ";
  }

  std::cout << "}" << std::endl;
}

int main()
{
  std::vector<int> v1 = {1, 2, 3};
  std::vector<int> v2 = {4, 5, 6};

  std::swap(v1, v2);

  print("v1", v1);
  print("v2", v2);
}

出力

v1 : {4 5 6 }
v2 : {1 2 3 }

参照