Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::valarray<T>::operator=

      From cppreference.com
      <cpp‎ |numeric‎ |valarray
       
       
       
      std::valarray
      Member functions
      Non-member functions
      Helper classes
      Deduction guides(C++17)
       
      valarray<T>& operator=(const valarray<T>& other);
      (1)
      valarray<T>& operator=( valarray<T>&& other)noexcept;
      (2)(since C++11)
      valarray<T>& operator=(const T& val);
      (3)
      valarray<T>& operator=(conststd::slice_array<T>& other);
      (4)
      valarray<T>& operator=(conststd::gslice_array<T>& other);
      (5)
      valarray<T>& operator=(conststd::mask_array<T>& other);
      (6)
      valarray<T>& operator=(conststd::indirect_array<T>& other);
      (7)
      valarray<T>& operator=(std::initializer_list<T> il);
      (8)(since C++11)

      Replaces the contents of the numeric array.

      1) Copy assignment operator. Ifsize()!= other.size(), first resizes*this as if byresize(other.size()). Each element of*this is assigned the value of the corresponding element ofother.
      2) Move assignment operator. Replaces the contents of*this with those ofother. The value ofother is unspecified after this operation. The complexity of this operation may be linear if T has non-trivial destructors, but is usually constant otherwise.
      3) Replaces each value in*this with a copy ofval.
      4-7) Replaces the contents of*this with the result of a generalized subscripting operation. The behavior is undefined ifsize() does not equal the length ofother or if any value on the left depends on the value on the right (e.g.v= v[v>2]).
      8) Assigns the contents of initializer listil. Equivalent to*this= valarray(il).

      Contents

      [edit]Parameters

      other - another numeric array (or a mask) to assign
      val - the value to initialize each element with
      il - initializer list to assign

      [edit]Return value

      *this

      [edit]Exceptions

      1,3-8) May throw implementation-defined exceptions.

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <valarray> void print(constchar* rem,conststd::valarray<int>& v){std::cout<<std::left<<std::setw(36)<< rem<<std::right;for(int n: v)std::cout<<std::setw(3)<< n;std::cout<<'\n';} int main(){std::valarray<int> v1(3);    v1=-1;// (3) from a scalar    print("assigned from scalar: ", v1);     v1={1,2,3,4,5,6};// (8): from initializer list of different size    print("assigned from initializer_list:", v1); std::valarray<int> v2(3);    v2= v1[std::slice(0,3,2)];// (4): from slice array    print("every 2nd element starting at pos 0:", v2);     v2= v1[v1%2==0];// (6): from mask array    print("values that are even:", v2); std::valarray<std::size_t> idx={0,1,2,4};// index array    v2.resize(4);// sizes must match when assigning from gen subscript    v2= v1[idx];// (7): from indirect array    print("values at positions 0, 1, 2, 4:", v2);}

      Output:

      assigned from scalar:                -1 -1 -1assigned from initializer_list:       1  2  3  4  5  6every 2nd element starting at pos 0:  1  3  5values that are even:                 2  4  6values at positions 0, 1, 2, 4:       1  2  3  5

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 624C++98the behaviors of overloads(4-7) were
      unclear if the length ofother is notsize()
      the behaviors are
      undefined in this case
      LWG 630C++98the behavior of the copy assignment operator
      was undefined ifsize()!= other.size()
      resizes*this
      first in this case
      LWG 2071C++11the move assignment operator resized
      *this ifsize()!= other.size()
      not required to
      resize in this case
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/valarray/operator%3D&oldid=149187"

      [8]ページ先頭

      ©2009-2025 Movatter.jp