Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::inplace_vector<T,N>::swap

      From cppreference.com
      <cpp‎ |container‎ |inplace vector
       
       
       
      std::inplace_vector
      Member types
      Member functions
      Non-member functions
       
      constexprvoid swap( inplace_vector& other)noexcept(/* see below */);
      (since C++26)

      Exchanges the contents of the container with those ofother. Does not cause iterators and references to associate with the other container.

      Contents

      [edit]Parameters

      other - container to exchange the contents with

      [edit]Return value

      (none)

      [edit]Exceptions

      noexcept specification:  

      [edit]Complexity

      Linear in size of the container.

      [edit]Example

      Run this code
      #include <inplace_vector>#include <print> int main(){std::inplace_vector<int,3> a1{1,2,3}, a2{4,5,6}; auto i1= a1.begin();auto i2= a2.begin();int& r1= a1[1];int& r2= a2[1]; auto print_them_all=[&](auto rem){std::println("{}a1 = {}, a2 = {}, *i1 = {}, *i2 = {}, r1 = {}, r2 = {}",                     rem, a1, a2,*i1,*i2, r1, r2);};     print_them_all("Before swap:\n");    a1.swap(a2);    print_them_all("After swap:\n"); // Note that after swap() iterators and references stay associated with their// original sites, e.g., i1 points to element a1[0], r1 refers to a1[1].}

      Output:

      Before swap:a1 = [1, 2, 3], a2 = [4, 5, 6], *i1 = 1, *i2 = 4, r1 = 2, r2 = 5After swap:a1 = [4, 5, 6], a2 = [1, 2, 3], *i1 = 4, *i2 = 1, r1 = 5, r2 = 2

      [edit]See also

      specializes thestd::swap algorithm
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/inplace_vector/swap&oldid=175276"

      [8]ページ先頭

      ©2009-2025 Movatter.jp