namespace std {
template <class T, class Allocator>
void
swap(list<T, Allocator>& x,
list<T, Allocator>& y); // (1) C++03
template <class T, class Allocator>
void
swap(list<T,Allocator>& x,
list<T,Allocator>& y)
noexcept(noexcept(x.swap(y))); // (1) C++17
template <class T, class Allocator>
constexpr void
swap(list<T,Allocator>& x,
list<T,Allocator>& y)
noexcept(noexcept(x.swap(y))); // (1) C++26
}
概要
2つのlistオブジェクトを入れ替える
効果
x.swap(y)
戻り値
なし
例
#include <iostream>
#include <list>
#include <string>
template <class T>
void print(const std::string& name, const std::list<T>& ls)
{
std::cout << name << " : {";
for (const T& x : ls) {
std::cout << x << ' ';
}
std::cout << "}" << std::endl;
}
int main()
{
std::list<int> ls1 = {4, 5, 6};
std::list<int> ls2 = {1, 2, 3};
std::swap(ls1, ls2);
::print("ls1", ls1);
::print("ls2", ls2);
}
出力
ls1 : {1 2 3 }
ls2 : {4 5 6 }
参照
- N4258 Cleaning-up noexcept in the Library, Rev 3
noexcept追加の経緯となる提案文書
- P3372R3 constexpr containers and adaptors