std::vector::swap(3)
π NAME π
okay bestie, welcome to the std::vector::swap(3) page from the C++ Standard Libary manual β yes, thatβs the manual name, and weβre keeping it exactly as it appears β¨
The name is literally:
std::vector::swap - std::vector::swap
thatβs it, thatβs the NAME section, no cap π
β¨ Synopsis β¨
Defined in header <vector> π
static void swap( reference x, reference y ); (until C++20)
constexpr static void swap( reference x, reference y ); (since C++20)
Swaps the contents of x and y as if by bool b = x; x = y; y = b;. fr fr, thatβs the exact stated recipe π₯
π Parameters π
xβstd::vector<bool>::referencevalue to swap withyyβstd::vector<bool>::referencevalue to swap withx
π«Ά Return value π«Ά
(none) β literally no return value, bestie π
β‘ Complexity β‘
Constant. β constant, periodt π
π Example π
Run this code, bestie:
// Run this code
#include <iostream>
#include <vector>
void println(std::string_view fmt, std::vector<bool> const& vb = {})
{
for (std::cout << fmt; bool const e : vb)
std::cout << e << ' ';
std::cout << '\n';
}
int main()
{
println("swap elements of the same vector:");
std::vector<bool> x{1, 0};
println("before swap, x: ", x);
x.swap(x[0], x[1]); // same as std::vector<bool>::swap(x[0], x[1]);
println("after swap, x: ", x);
println("swap elements of two different vectors:");
std::vector<bool> y{0, 0, 1};
println("before swap, x: ", x);
println("before swap, y: ", y);
y.swap(x[0], y[2]); // same as std::vector<bool>::swap(x[0], y[2]);
println("after swap, x: ", x);
println("after swap, y: ", y);
}
π Output π
swap elements of the same vector:
before swap, x: 1 0
after swap, x: 0 1
swap elements of two different vectors:
before swap, x: 0 1
before swap, y: 0 0 1
after swap, x: 1 1
after swap, y: 0 0 0
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 814β applied to C++98; behavior as published: the description of this member function was missing; correct behavior: added.
π See also π
referenceβ proxy class representing a reference to a single bool (class)swapβ swaps the contents (public member function ofstd::vector<T,Allocator>)std::swap(std::vector)β specializes thestd::swapalgorithm (function template)
xoxo, your std::vector::swap(3) bestie β source/version http://cppreference.com, dated 2024.06.10 π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms