Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::swap(std::basic_string)

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      Defined in header<string>
      template<class CharT,class Traits,class Alloc>

      void swap(std::basic_string<CharT, Traits, Alloc>& lhs,

                 std::basic_string<CharT, Traits, Alloc>& rhs);
      (until C++17)
      template<class CharT,class Traits,class Alloc>

      void swap(std::basic_string<CharT, Traits, Alloc>& lhs,

                 std::basic_string<CharT, Traits, Alloc>& rhs)noexcept(/* see below */);
      (since C++17)
      (constexpr since C++20)

      Specializes thestd::swap algorithm forstd::basic_string. Swaps the contents oflhs andrhs.

      Equivalent tolhs.swap(rhs).

      Contents

      [edit]Parameters

      lhs, rhs - strings whose contents to swap

      [edit]Return value

      (none)

      [edit]Complexity

      Constant.

      Exceptions

      noexcept specification:  
      noexcept(noexcept(lhs.swap(rhs)))
      (since C++17)

      [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 = AAAb = BBBB After swap:a = BBBBb = AAA

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2064C++11non-memberswap was noexcept and inconsistent with memberswapnoexcept removed

      [edit]See also

      swaps the contents
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/swap2&oldid=171031"

      [8]ページ先頭

      ©2009-2025 Movatter.jp