• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function template
    <string>

    std::swap (非メンバ関数)

    namespace std {
      template <class CharT, class Traits, class Allocator>
      void swap(basic_string<CharT, Traits, Allocator>& x,
                basic_string<CharT, Traits, Allocator>& y); // (1) C++03
      template <class CharT, class Traits, class Allocator>
      void swap(basic_string<CharT, Traits, Allocator>& x,
                basic_string<CharT, Traits, Allocator>& y)
        noexcept(noexcept(lhs.swap(rhs)));                 // (1) C++17
      template <class CharT, class Traits, class Allocator>
      constexpr
      void swap(basic_string<CharT, Traits, Allocator>& x,
                basic_string<CharT, Traits, Allocator>& y)
        noexcept(noexcept(lhs.swap(rhs)));                 // (1) C++20
    }
    

    概要

    2つのbasic_stringオブジェクトを入れ替える

    効果

    x.swap(y)

    戻り値

    なし

    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string a = "hello";
      std::string b = "world";
    
      std::swap(a, b);
    
      std::cout << a << std::endl;
      std::cout << b << std::endl;
    }
    

    出力

    world
    hello
    

    参照