std::swap(std::basic_string)
From cppreference.com
 
 
 < cpp | string | basic string 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
std::basic_string 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Literals  Helper classes  Deduction guides (C++17) 
(C++23)
  (DR*)
(DR*)
(C++23)
(DR*)
(C++23)
(C++23)
(C++20)
(C++20)
(C++23)
swap(std::basic_string)
(C++20)(C++20)
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
(C++11)(C++11)(C++11)
(C++11)(C++11)
 (C++14)
(C++11)
Defined in header 
 
 
<string> 
 template< class CharT, class Traits, class Alloc >
 
 (until C++17)
void swap( std::basic_string <CharT, Traits, Alloc>& lhs,
template< class CharT, class Traits, class Alloc >
 
 (since C++17) void swap( std::basic_string <CharT, Traits, Alloc>& lhs,
(constexpr since C++20)
Specializes the std::swap algorithm for std::basic_string . Swaps the contents of lhs and rhs.
Equivalent to lhs.swap(rhs).
Contents
[edit] Parameters
 lhs, rhs
 -
 strings whose contents to swap
[edit] Return value
(none)
[edit] Complexity
Constant.
[edit] Example
Run this code
#include <iostream> #include <string> int main() { std::string a = "AAA"; std::string b = "BBBB"; std::cout << "Before swap:\n" "a = " << a << "\n" "b = " << b << "\n\n"; std::swap (a, b); std::cout << "After swap:\n" "a = " << a << "\n" "b = " << b << '\n'; }
Output:
Before swap: a = AAA b = BBBB After swap: a = BBBB b = AAA
[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 2064 | C++11 | non-member swapwas noexcept and inconsistent with memberswap | noexcept removed |