Namespaces
Variants

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

From cppreference.net
헤더 파일에 정의됨 <vector>
static void swap ( reference x, reference y ) ;
(C++20부터 constexpr)

x y 의 내용을 마치 bool b = x ; x = y ; y = b ; 와 같이 교환합니다.

목차

매개변수

x - std::vector < bool > :: reference 와 교환할 값 y
y - std::vector < bool > :: reference 와 교환할 값 x

반환값

(없음)

복잡도

상수.

예제

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

출력:

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

결함 보고서

다음 동작 변경 결함 보고서는 이전에 게시된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 814 C++98 이 멤버 함수에 대한 설명이 누락됨 추가됨

참고 항목

단일 bool 에 대한 참조를 나타내는 프록시 클래스
(클래스)
내용을 교환
( std::vector<T,Allocator> 의 public 멤버 함수)
std::swap 알고리즘을 특수화
(함수 템플릿)