- vector[meta header]
- std[meta namespace]
- vector[meta class]
- function[meta id-type]
void swap(vector& x); // (1) C++03
void swap(vector& x) // (1) C++17
noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value
|| allocator_traits<Allocator>::is_always_equal::value);
constexpr void swap(vector& x) // (1) C++20
noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value
|| allocator_traits<Allocator>::is_always_equal::value);- allocator_traits[link /reference/memory/allocator_traits.md]
他のvectorオブジェクトとデータを入れ替える。
*thisの内容とcapacity()をxと交換する。
なし
定数時間
swapされるコンテナの要素を指す参照、ポインタ、イテレータを無効化しない。
(ただし、end()が指すイテレータはどの要素も指していないが、無効になることがある。)
#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};
v1.swap(v2);
print("v1", v1);
print("v2", v2);
}- swap[color ff0000]
v1 : {4 5 6 }
v2 : {1 2 3 }
- N4258 Cleaning-up noexcept in the Library, Rev 3
noexcept追加の経緯となる提案文書
- P1004R2 Making
std::vectorconstexpr