Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::swap(std::tuple)

      From cppreference.com
      <cpp‎ |utility‎ |tuple
       
       
      Utilities library
       
       
      Defined in header<tuple>
      (1)
      template<class...Types>

      void swap(std::tuple<Types...>& lhs,

                 std::tuple<Types...>& rhs)noexcept(/* see below */);
      (since C++11)
      (until C++20)
      template<class...Types>

      constexprvoid swap(std::tuple<Types...>& lhs,

                           std::tuple<Types...>& rhs)noexcept(/* see below */);
      (since C++20)
      template<class...Types>

      constexprvoid swap(conststd::tuple<Types...>& lhs,

                           conststd::tuple<Types...>& rhs)noexcept(/* see below */);
      (2)(since C++23)

      Swaps the contents oflhs andrhs. Equivalent tolhs.swap(rhs).

      1) This overload participates in overload resolution only ifstd::is_swappable_v<Ti> istrue for all i from 0 tosizeof...(Types).
      2) This overload participates in overload resolution only ifstd::is_swappable_v<const Ti> istrue for all i from 0 tosizeof...(Types).
      (since C++17)

      Contents

      [edit]Parameters

      lhs, rhs - tuples whose contents to swap

      [edit]Return value

      (none)

      [edit]Exceptions

      noexcept specification:  
      noexcept(noexcept(lhs.swap(rhs)))

      [edit]Example

      Run this code
      #include <iostream>#include <string>#include <tuple> int main(){std::tuple<int,std::string,float> p1{42,"ABCD",2.71}, p2;    p2=std::make_tuple(10,"1234",3.14); auto print_p1_p2=[&](auto rem){std::cout<< rem<<"p1 = {"<< std::get<0>(p1)<<", "<< std::get<1>(p1)<<", "<< std::get<2>(p1)<<"}, "<<"p2 = {"<< std::get<0>(p2)<<", "<< std::get<1>(p2)<<", "<< std::get<2>(p2)<<"}\n";};     print_p1_p2("Before p1.swap(p2): ");    p1.swap(p2);    print_p1_p2("After  p1.swap(p2): ");    swap(p1, p2);    print_p1_p2("After swap(p1, p2): ");}

      Output:

      Before p1.swap(p2): p1 = {42, ABCD, 2.71}, p2 = {10, 1234, 3.14}After  p1.swap(p2): p1 = {10, 1234, 3.14}, p2 = {42, ABCD, 2.71}After swap(p1, p2): p1 = {42, ABCD, 2.71}, p2 = {10, 1234, 3.14}

      [edit]See also

      swaps the contents of twotuples
      (public member function)[edit]
      specializes thestd::swap algorithm
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/tuple/swap2&oldid=160687"

      [8]ページ先頭

      ©2009-2025 Movatter.jp