Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/vector bool/swap"

From cppreference.com
(P1004R2)
m (fmt)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{cpp/container/vector_bool/title | swap}}
+
{{cpp/container/vector_bool/title|swap}}
 
{{cpp/container/vector_bool/navbar}}
 
{{cpp/container/vector_bool/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | vector}}
+
{{dcl header|vector}}
{{dcl rev multi |
+
{{dcl rev multi||dcl1=
| dcl1=
+
static void swap( reference x, reference y );
static void swap(reference x, reference y);
+
|since2=c++20|dcl2=
| since2=c++20 | dcl2=
+
constexpr static void swap( reference x, reference y );
constexpr static void swap(reference x, reference y);
+
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Swaps the contents of {{tt|x}} and {{tt|y}}.
+
Swaps the contents of {{c|x}} and {{c|y}} as if by {{c|1=bool b = x; x = y; y = b;}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | x | {{c|std::vector<bool>::reference}} value to swap with {{tt|y}}}}
+
{{par|x|{{rlpt|/|std::vector}}{{c/core|<bool>::}}{{rlpt|reference}} value to swap with {{c|y}}}}
{{par | y | {{c|std::vector<bool>::reference}} value to swap with {{tt|x}}}}
+
{{par|y|{{rlpt|/|std::vector}}{{c/core|<bool>::}}{{rlpt|reference}} value to swap with {{c|x}}}}
 
{{par end}}
 
{{par end}}
  
Line 26: Line 25:
  
 
===Example===
 
===Example===
{{example|
+
{{example
 
|code=
 
|code=
#include <vector>
 
 
#include <iostream>
 
#include <iostream>
 +
#include <vector>
  
int main()
+
void println(std::string_view fmt, std::vector<bool> const& vb = {})
 
{
 
{
     std::vector<bool> vb1{ 1,0 };
+
     for (std::cout << fmt; bool const e : vb)
 
+
        std::cout << e << ' ';
    for (auto e : vb1) { std::cout << e << " "; }
+
 
     std::cout << '\n';
 
     std::cout << '\n';
 +
}
  
     vb1.swap(vb1[0], vb1[1]);
+
int main()
 
+
{
     for (auto e : vb1) { std::cout << e << " "; }
+
    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=
 
|output=
1 0  
+
swap elements of the same vector:
0 1  
+
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===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=814|std=C++98|before=the description of this member function was missing|after=added}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc class | cpp/container/vector_bool/reference | proxy class representing a reference to a single bool}}
+
{{dsc class|cpp/container/vector bool/reference|proxy class representing a reference to a single {{c/core|bool}}}}
{{dsc inc | cpp/container/dsc swap | vector}}
+
{{dsc inc|cpp/container/dsc swap|vector}}
{{dsc inc | cpp/container/dsc swap2 | vector}}
+
{{dsc inc|cpp/container/dsc swap2|vector}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 11:34, 5 November 2023

 
 
 
 
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;.

Contents

[edit] Parameters

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

[edit] Return value

(none)

[edit] Complexity

Constant.

[edit] 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);
}

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.

DR Applied to Behavior as published Correct behavior
LWG 814 C++98 the description of this member function was missing added

[edit] See also

proxy class representing a reference to a single bool
(class)
swaps the contents
(public member function of std::vector<T,Allocator>) [edit]
specializes the std::swap algorithm
(function template) [edit]