Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |container‎ |vector bool
       
       
       
      std::vector<bool>
      Member types
      Member functions
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)
      Helper classes
      Deduction guides(C++17)
       
      Defined in header<vector>
      staticvoid swap( reference x, reference y);
      (constexpr since C++20)

      Swaps the contents ofx andy as if bybool b= x; x= y; y= b;.

      Contents

      [edit]Parameters

      x -std::vector<bool>::reference value to swap withy
      y -std::vector<bool>::reference value to swap withx

      [edit]Return value

      (none)

      [edit]Complexity

      Constant.

      [edit]Example

      Run this code
      #include <iostream>#include <vector> void println(std::string_view fmt,std::vector<bool>const& vb={}){for(std::cout<< fmt;boolconst 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

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

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

      [edit]See also

      proxy class representing a reference to a singlebool
      (class)
      swaps the contents
      (public member function ofstd::vector<T,Allocator>)[edit]
      specializes thestd::swap algorithm
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/vector_bool/swap&oldid=178319"

      [8]ページ先頭

      ©2009-2025 Movatter.jp