Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::inplace_vector<T,N>::operator=

      From cppreference.com
      <cpp‎ |container‎ |inplace vector
       
       
       
      std::inplace_vector
      Member types
      Member functions
      Non-member functions
       
      constexpr inplace_vector& operator=(const inplace_vector& other);
      (1)(since C++26)
      constexpr inplace_vector& operator=( inplace_vector&& other)
         noexcept(/* see below */);
      (2)(since C++26)
      constexpr inplace_vector& operator=(std::initializer_list<T> init);
      (3)(since C++26)

      Replaces the contents of theinplace_vector.

      1)Copy assignment operator. Also atrivial copy assignment operator, ifstd::inplace_vector<T, N> has atrivial destructor, andstd::is_trivially_copy_constructible_v<T>&&std::is_trivially_copy_assignable_v<T> istrue. Replaces the contents with a copy of the contents ofother.
      2)Move assignment operator. Also atrivial move assignment operator, ifstd::inplace_vector<T, N> has atrivial destructor, andstd::is_trivially_move_constructible_v<T>&&std::is_trivially_move_assignable_v<T> istrue. Replaces the contents with those ofother using move semantics (i.e., the data inother is moved fromother into this container).other is in a valid but unspecified state afterwards.
      3) Replaces the contents with those identified by initializer listinit.

      Contents

      [edit]Parameters

      other - anotherinplace_vector to be used as source to initialize the elements of the container with
      init - initializer list to initialize the elements of the container with

      [edit]Complexity

      1,2) Linear in the size of*this andother.
      3) Linear in the size of*this andinit.

      [edit]Exceptions

      2)
      noexcept specification:  
      noexcept(N==0||

             (std::is_nothrow_move_assignable_v<T>&&

               std::is_nothrow_move_constructible_v<T>))
      3) Throwsstd::bad_alloc ifinit.size()> N.

      [edit]Example

      Run this code
      #include <initializer_list>#include <inplace_vector>#include <new>#include <print>#include <ranges>#include <string> int main(){std::inplace_vector<int,4> x({1,2,3}), y;std::println("Initially:");std::println("x = {}", x);std::println("y = {}", y); std::println("Copy assignment copies data from x to y:");    y= x;// overload (1)std::println("x = {}", x);std::println("y = {}", y); std::inplace_vector<std::string,3> z, w{"\N{CAT}","\N{GREEN HEART}"};std::println("Initially:");std::println("z = {}", z);std::println("w = {}", w); std::println("Move assignment moves data from w to z:");    z= std::move(w);// overload (2)std::println("z = {}", z);std::println("w = {}", w);// w is in valid but unspecified state auto l={4,5,6,7};std::println("Assignment of initializer_list {} to x:", l);    x= l;// overload (3)std::println("x = {}", x); std::println("Assignment of initializer_list with size bigger than N throws:");try{        x={1,2,3,4,5};// throws: (initializer list size == 5) > (capacity N == 4)}catch(conststd::bad_alloc& ex){std::println("ex.what(): {}", ex.what());}}

      Possible output:

      Initially:x = [1, 2, 3]y = []Copy assignment copies data from x to y:x = [1, 2, 3]y = [1, 2, 3]Initially:z = []w = ["🐈", "💚"]Move assignment moves data from w to z:z = ["🐈", "💚"]w = ["", ""]Assignment of initializer_list [4, 5, 6, 7] to x:x = [4, 5, 6, 7]Assignment of initializer_list with size bigger than N throws:ex.what(): std::bad_alloc

      [edit]See also

      constructs theinplace_vector
      (public member function)[edit]
      assigns values to the container
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/inplace_vector/operator%3D&oldid=175717"

      [8]ページ先頭

      ©2009-2025 Movatter.jp