Difference between revisions of "cpp/container/vector bool/swap"
From cppreference.com
< cpp | container | vector bool
(P1004R2) |
Andreas Krug (Talk | contribs) 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 {{ | + | 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 | {{ | + | {{par|x|{{rlpt|/|std::vector}}{{c/core|<bool>::}}{{rlpt|reference}} value to swap with {{c|y}}}} |
− | {{par | y | {{ | + | {{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 <iostream> | #include <iostream> | ||
+ | #include <vector> | ||
− | + | void println(std::string_view fmt, std::vector<bool> const& vb = {}) | |
{ | { | ||
− | std:: | + | for (std::cout << fmt; bool const e : vb) |
− | + | std::cout << e << ' '; | |
− | + | ||
std::cout << '\n'; | 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= | |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/ | + | {{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
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
[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> )
| |
specializes the std::swap algorithm (function template) |