table of contents
        
      
      
    | std::basic_string::swap(3) | C++ Standard Libary | std::basic_string::swap(3) | 
NAME¶
std::basic_string::swap - std::basic_string::swap
Synopsis¶
 void swap( basic_string& other ); (until C++17)
  
   void swap( basic_string& other ) noexcept(/* see below */); (since
    C++17)
  
   (until C++20)
  
   constexpr void swap( basic_string& other ) noexcept(/* see below */);
    (since C++20)
  
   Exchanges the contents of the string with those of other. All iterators and
  
   references may be invalidated.
  
   The behavior is undefined if Allocator does not propagate on swap and
    (since C++11)
  
   the allocators of *this and other are unequal.
Parameters¶
other - string to exchange the contents with
Return value¶
(none)
Complexity¶
Constant.
Exceptions¶
 (since
  
   noexcept specification: C++17)
  
  
    noexcept(std::allocator_traits<Allocator>::propagate_on_container_swap::value
  
   || std::allocator_traits<Allocator>::is_always_equal::value)
Example¶
// Run this code
  
   #include <string>
  
   #include <iostream>
  
   int main()
  
   {
  
   std::string a = "AAA";
  
   std::string b = "BBB";
  
   std::cout << "before swap" << '\n';
  
   std::cout << "a: " << a << '\n';
  
   std::cout << "b: " << b << '\n';
  
   a.swap(b);
  
   std::cout << "after swap" << '\n';
  
   std::cout << "a: " << a << '\n';
  
   std::cout << "b: " << b << '\n';
  
   }
Output:¶
 before swap
  
   a: AAA
  
   b: BBB
  
   after swap
  
   a: BBB
  
   b: AAA
See also¶
 swap swaps the values of two objects
  
   (function template)
  
   swap_ranges swaps two ranges of elements
  
   (function template)
  
   swap swaps the contents
  
   (C++17) (public member function of
    std::basic_string_view<CharT,Traits>)
| 2022.07.31 | http://cppreference.com |