std::vector<bool,Allocator>::swap

Header: <vector>

Swaps the contents of x and y as if by bool b = x; x = y; y = b;.

# Declarations

static void swap( reference x, reference y );

(constexpr since C++20)

# Parameters

# Return value

(none)

# Example

#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);
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 814C++98the description of this member function was missingadded

# See also