valarray 類別

類別範本 valarray<Type> 描述一個 物件,該物件控制儲存為陣列、專為高速數學運算所設計,以及針對計算效能優化的類型 Type 元素序列。

備註

類別是已排序值集數學概念的表示法。 其元素會依序從零編號。 類別描述為 接近容器 ,因為它支援一些功能,但不支援第一級序列容器的功能,例如 vector, 支援。 它與類別範本 vector 不同,有兩個重要方式:

  • 它會定義相同型別和長度之物件對應項目 valarray<Type> 之間的許多算術運算,例如 varr_x* = cos(varr_y) + sin(varr_z)。

  • 它會藉由多載 operator[]來定義各種有趣的方式來下標 valarray<Type> 物件。

類別 Type的物件:

  • 具有公用預設建構函式、解構函式、複製建構函式和指派運算子,且這些都具有傳統行為。

  • 視需要定義算術運算子和數學函式,這些函式定義為具有傳統行為的浮點類型。

特別是在指派之後,細微的差異可能不存在於複製建構和預設建構之間。 類別對 Type 象上沒有任何作業可能會擲回例外狀況。

成員

建構函式

名稱 描述
valarray 建構具有特定大小或具有特定值之項目的 valarray,或建構為另一個 valarray 的複本或另一個 valarray 的子集。

Typedefs

名稱 描述
value_type 代表儲存在 valarray 中之項目類型的類型。

函式

名稱 描述
apply 將指定的函式套用至 valarray 的每個項目。
cshift 定期依指定的位置數目將 valarray 中的所有項目移位。
free 釋放 valarray 所使用的記憶體。
max 尋找 valarray 中的最大項目。
min 尋找 valarray 中的最小項目。
resize 將 valarray 中的項目數目變更為指定的數目,視需要加入或移除項目。
shift 依指定的位置數目將 valarray 中的所有項目移位。
size 尋找 valarray 中的項目數目。
sum 判斷非零長度的 valarray 中所有項目的總和。
swap

操作員

名稱 描述
operator! 一元運算符,可取得 中valarray每個項目的邏輯 NOT (!) 值。
operator%= 取得陣列項目除以指定的 valarray 或除以此項目類型之值的餘數。
operator&= 取得陣列中專案的位 AND (&) ,其具有指定 valarray 中對應的專案,或是具有專案類型的值。
operator>>= 依位置的指定數目或依第二個 valarray 指定的項目數量,將 valarray 運算元的每個項目之位元右移。
operator<<= 依位置的指定數目或依第二個 valarray 指定的項目數量,將 valarray 運算元的每個項目之位元左移。
operator*= 將指定之 valarray 的項目或此項目類型的值和運算元 valarray 相乘。
operator+ 對 valarray 中每個項目套用加號的一元運算子。 在一般算術類型上,此運算符沒有任何作用。
operator+= 將指定之 valarray 的項目或此項目類型的值加到運算元 valarray。
operator- 將算術否定套用至 中 valarray每個專案的一元運算元。
operator-= 將運算元 valarray 減去指定之 valarray 的項目或此項目類型的值。
operator/= 將運算元 valarray 除以指定之 valarray 的項目或此項目類型的值。
operator= 指派項目至 valarray,其值直接受到指定或指定為其他 valarray 的一部份,或由 slice_array、gslice_array、mask_array 或 indirect_array 指定。
operator[] 傳回項目參考或其指定索引處的值或指定的子集。
operator^= 取得具有指定valarray或專案型別值之的位獨佔 或 ()。^valarray
operator|= 取得陣列中專案的位 OR (|) ,其具有指定 valarray 中對應的專案,或是具有專案類型的值。
operator~ 一元運算符,可取得 中valarray每個專案的位補碼 (~) 值。

apply

將指定的函式套用至 valarray 的每個項目。

valarray<Type> apply(Type function_object(Type)) const;

valarray<Type> apply(Type function_object(const Type&)) const;

參數

function_object(Type)
要套用至操作數 valarray之每個專案的函式物件。

function_object(const Type&)
要套用至操作數valarray之每個專案的函式物件const。

傳回值

valarray,其專案已function_object將專案套用至操作數 valarray的專案。

備註

成員函式會傳回 類別 valarray<Type>的物件,其 size每個元素 elem 都是 的結果 function_object((*this)[elem])。

範例

// valarray_apply.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

using namespace std;

int __cdecl MyApplyFunc( int n )
{
   return n*2;
}

int main( int argc, char* argv[] )
{
   valarray<int> vaR(10), vaApplied(10);
   int i;

   for ( i = 0; i < 10; i += 3 )
      vaR[i] = i;

   for ( i = 1; i < 10; i += 3 )
      vaR[i] = 0;

   for ( i = 2; i < 10; i += 3 )
      vaR[i] = -i;

   cout << "The initial Right valarray is: (";
   for   ( i=0; i < 10; ++i )
      cout << " " << vaR[i];
   cout << " )" << endl;

   vaApplied = vaR.apply( MyApplyFunc );

   cout << "The element-by-element result of "
       << "applying MyApplyFunc to vaR is the\nvalarray: ( ";
   for ( i = 0; i < 10; ++i )
      cout << " " << vaApplied[i];
   cout << " )" << endl;
}
The initial Right valarray is: ( 0 0 -2 3 0 -5 6 0 -8 9 )
The element-by-element result of applying MyApplyFunc to vaR is the
valarray: (  0 0 -4 6 0 -10 12 0 -16 18 )

cshift

定期依指定的位置數目將 valarray 中的所有項目移位。

valarray<Type> cshift(int count) const;

參數

count
項目要向前移位的位置數目。

傳回值

新的 valarray ,與操作valarray數中的位置相較之下,所有元素都以迴圈方式向前valarray移動,或向左移動count。

備註

正值的 count 會將項目循環左移 count 個位置。

負值的 count 會將項目循環右移 count 個位置。

範例

// valarray_cshift.cpp
// compile with: /EHsc

#include <valarray>
#include <iostream>

int main()
{
    using namespace std;
    int i;

    valarray<int> va1(10), va2(10);
    for (i = 0; i < 10; i+=1)
        va1[i] = i;
    for (i = 0; i < 10; i+=1)
        va2[i] = 10 - i;

    cout << "The operand valarray va1 is: (";
    for (i = 0; i < 10; i++)
        cout << " " << va1[i];
    cout << ")" << endl;

    // A positive parameter shifts elements right
    va1 = va1.cshift(4);
    cout << "The cyclically shifted valarray va1 is:\nva1.cshift (4) = (";
    for (i = 0; i < 10; i++)
        cout << " " << va1[i];
    cout << ")" << endl;

    cout << "The operand valarray va2 is: (";
    for (i = 0; i < 10; i++)
        cout << " " << va2[i];
    cout << ")" << endl;

    // A negative parameter shifts elements left
    va2 = va2.cshift(-4);
    cout << "The cyclically shifted valarray va2 is:\nva2.shift (-4) = (";
    for (i = 0; i < 10; i++)
        cout << " " << va2[i];
    cout << ")" << endl;
}
The operand valarray va1 is: ( 0 1 2 3 4 5 6 7 8 9)
The cyclically shifted valarray va1 is:
va1.cshift (4) = ( 4 5 6 7 8 9 0 1 2 3)
The operand valarray va2 is: ( 10 9 8 7 6 5 4 3 2 1)
The cyclically shifted valarray va2 is:
va2.shift (-4) = ( 4 3 2 1 10 9 8 7 6 5)

free

釋放 valarray 所使用的記憶體。

void free();

備註

這個非標準函式相當於指派空 valarray的 。 例如:

valarray<T> v;
v = valarray<T>();

// equivalent to v.free()

max

尋找 valarray 中的最大項目。

Type max() const;

傳回值

操作數 valarray中專案的最大值。

備註

成員函式會藉由在 類別Type的元素組之間套用運算符<或運算元>,藉此比較值,而必須針對元素Type提供運算符。

範例

// valarray_max.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i, MaxValue;

   valarray<int> vaR ( 10 );
   for ( i = 0 ; i < 10 ; i += 3 )
      vaR [ i ] =  i;
   for ( i = 1 ; i < 10 ; i += 3 )
      vaR [ i ] =  2*i - 1;
   for ( i = 2 ; i < 10 ; i += 3 )
      vaR [ i ] =  10 - i;

   cout << "The operand valarray is: ( ";
      for (i = 0 ; i < 10 ; i++ )
         cout << vaR [ i ] << " ";
   cout << ")." << endl;

   MaxValue = vaR.max (  );
   cout << "The largest element in the valarray is: "
        << MaxValue  << "." << endl;
}
The operand valarray is: ( 0 1 8 3 7 5 6 13 2 9 ).
The largest element in the valarray is: 13.

min

尋找 valarray 中的最小項目。

Type min() const;

傳回值

操作數 valarray中專案的最小值。

備註

成員函式會藉由在 類別Type的元素組之間套用運算符<或運算元>,藉此比較值,而必須針對元素Type提供運算符。

範例

// valarray_min.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i, MinValue;

   valarray<int> vaR ( 10 );
   for ( i = 0 ; i < 10 ; i += 3 )
      vaR [ i ] =  -i;
   for ( i = 1 ; i < 10 ; i += 3 )
      vaR [ i ] =  2*i;
   for ( i = 2 ; i < 10 ; i += 3 )
      vaR [ i ] =  5 - i;

   cout << "The operand valarray is: ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << vaR [ i ] << " ";
   cout << ")." << endl;

   MinValue = vaR.min ( );
   cout << "The smallest element in the valarray is: "
        << MinValue  << "." << endl;
}
/* Output:
The operand valarray is: ( 0 2 3 -3 8 0 -6 14 -3 -9 ).
The smallest element in the valarray is: -9.
*/

operator!

一元運算符,可取得 中valarray每個項目的邏輯 NOT (!) 值。

valarray<bool> operator!() const;

傳回值

布爾值的 , valarray 這些值是操作數 valarray之專案值的邏輯否定。

備註

邏輯運算 NOT,以 !邏輯方式否定元素,因為它會將所有零值 true 轉換成,並將所有非零值轉換成 false。 布爾值傳 valarray 回的大小與操作數 valarray相同。

範例

// valarray_op_lognot.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> vaL ( 10 );
   valarray<bool> vaNOT ( 10 );
   for ( i = 0 ; i < 10 ; i += 2 )
      vaL [ i ] =  0;
   for ( i = 1 ; i < 10 ; i += 2 )
      vaL [ i ] =  i-1;

   cout << "The initial valarray is:  ( ";
      for (i = 0 ; i < 10 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;

   vaNOT = !vaL;
   cout << "The element-by-element result of "
        << "the logical NOT operator! is the"
        << endl << "valarray: ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << vaNOT [ i ] << " ";
   cout << ")." << endl;
}
The initial valarray is:  ( 0 0 0 2 0 4 0 6 0 8 ).
The element-by-element result of the logical NOT operator! is the
valarray: ( 1 1 1 0 1 0 1 0 1 0 ).

operator%=

取得陣列項目除以指定的 valarray 或除以此項目類型之值的餘數。

valarray<Type>& operator%=(const valarray<Type>& right);

valarray<Type>& operator%=(const Type& right);

參數

right
專案 valarray 型別的 或 值,與要除除操作數的操作數 valarray 相同,以元素為根據,操作數 valarray。

傳回值

valarray,其專案是 操作數valarrayright之元素相除的餘數。

範例

// valarray_class_op_rem.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> vaL ( 6 ), vaR ( 6 );
   for ( i = 0 ; i < 6 ; i += 2 )
      vaL [ i ] =  53;
   for ( i = 1 ; i < 6 ; i += 2 )
      vaL [ i ] =  -67;
   for ( i = 0 ; i < 6 ; i++ )
      vaR [ i ] =  3*i+1;

   cout << "The initial valarray is: ( ";
      for ( i = 0 ; i < 6 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;

   cout << "The initial  right valarray is: ( ";
      for ( i = 0 ; i < 6 ; i++ )
         cout << vaR [ i ] << " ";
   cout << ")." << endl;

   vaL %= vaR;
   cout << "The remainders from the element-by-element "
        << "division is the"
        << endl << "valarray: ( ";
      for ( i = 0 ; i < 6 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;
}
The initial valarray is: ( 53 -67 53 -67 53 -67 ).
The initial  right valarray is: ( 1 4 7 10 13 16 ).
The remainders from the element-by-element division is the
valarray: ( 0 -3 4 -7 1 -3 ).

operator&=

取得陣列中專案的位 AND (&) ,其具有指定 valarray 中對應的專案,或是具有專案類型的值。

valarray<Type>& operator&=(const valarray<Type>& right);

valarray<Type>& operator&=(const Type& right);

參數

right
專案 valarray 型別的 或 值,與操作數相同的操作數 valarray ,由位AND (&) 與操作數 valarray結合。

傳回值

valarray,其專案為操作數位valarray AND (&)right

備註

位運算只能用來操作整數位資料類型中的位,例如 char 和 int。 它不適用於 float、 double、 long double、 void、、 bool或其他更複雜的數據類型。

位 AND (&) 適用於個別位層級上的數據類型。 如果兩個位b1b2b1 & b2都是 1,則為 1;如果至少有一個位為 0,則為 0。

範例

// valarray_class_op_bitand.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> vaL ( 10 ), vaR ( 10 );
   for ( i = 0 ; i < 10 ; i += 2 )
      vaL [ i ] =  0;
   for ( i = 1 ; i < 10 ; i += 2 )
      vaL [ i ] =  i-1;
   for ( i = 0 ; i < 10 ; i++ )
      vaR [ i ] =  i;

   cout << "The initial valarray is:  ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;

   cout << "The initial Right valarray is: ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << vaR [ i ] << " ";
   cout << ")." << endl;

   vaL &= vaR;
   cout << "The element-by-element result of "
        << "the bitwise AND operator&= is the"
        << endl << "valarray: ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;
}
The initial valarray is:  ( 0 0 0 2 0 4 0 6 0 8 ).
The initial Right valarray is: ( 0 1 2 3 4 5 6 7 8 9 ).
The element-by-element result of the bitwise AND operator&= is the
valarray: ( 0 0 0 2 0 4 0 6 0 8 ).

operator>>=

依位置的指定數目或依第二個 valarray 指定的項目數量,將 valarray 運算元的每個項目之位元右移。

valarray<Type>& operator>>=(const valarray<Type>& right);

valarray<Type>& operator>>=(const Type& right);

參數

right
值,指出右移位的數量,或 valarray 其專案表示右移的專案量。

傳回值

, valarray 其專案已向右移中指定 right的數量。

備註

帶正負號的數字會保留其符號。

範例

// valarray_class_op_rs.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> vaL ( 8 ), vaR ( 8 );
   for ( i = 0 ; i < 8 ; i += 2 )
      vaL [ i ] =  64;
   for ( i = 1 ; i < 8 ; i += 2 )
      vaL [ i ] =  -64;
   for ( i = 0 ; i < 8 ; i++ )
      vaR [ i ] =  i;

   cout << "The initial operand valarray is: ( ";
      for ( i = 0 ; i < 8 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;

   cout << "The  right valarray is: ( ";
      for ( i = 0 ; i < 8 ; i++ )
         cout << vaR [ i ] << " ";
   cout << ")." << endl;

   vaL >>= vaR;
   cout << "The element-by-element result of "
        << "the right shift is the"
        << endl << "valarray: ( ";
      for ( i = 0 ; i < 8 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;
}
The initial operand valarray is: ( 64 -64 64 -64 64 -64 64 -64 ).
The  right valarray is: ( 0 1 2 3 4 5 6 7 ).
The element-by-element result of the right shift is the
valarray: ( 64 -32 16 -8 4 -2 1 -1 ).

operator<<=

依位置的指定數目或依第二個 valarray 指定的項目數量,將 valarray 運算元的每個項目之位元左移。

valarray<Type>& operator<<=(const valarray<Type>& right);

valarray<Type>& operator<<=(const Type& right);

參數

right
值,表示左移的數量,或其 valarray 專案表示左移的專案量。

傳回值

valarray,其專案已移出 中指定的right數量。

備註

帶正負號的數字會保留其符號。

範例

// valarray_class_op_ls.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> vaL ( 8 ), vaR ( 8 );
   for ( i = 0 ; i < 8 ; i += 2 )
      vaL [ i ] =  1;
   for ( i = 1 ; i < 8 ; i += 2 )
      vaL [ i ] =  -1;
   for ( i = 0 ; i < 8 ; i++ )
      vaR [ i ] =  i;

   cout << "The initial operand valarray is: ( ";
      for ( i = 0 ; i < 8 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;

   cout << "The  right valarray is: ( ";
      for ( i = 0 ; i < 8 ; i++ )
         cout << vaR [ i ] << " ";
   cout << ")." << endl;

   vaL <<= vaR;
   cout << "The element-by-element result of "
        << "the left shift"
        << endl << "on the operand array is the valarray:"
        << endl << "( ";
      for ( i = 0 ; i < 8 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;
}
The initial operand valarray is: ( 1 -1 1 -1 1 -1 1 -1 ).
The  right valarray is: ( 0 1 2 3 4 5 6 7 ).
The element-by-element result of the left shift
on the operand array is the valarray:
( 1 -2 4 -8 16 -32 64 -128 ).

operator*=

將指定之 valarray 的項目或此項目類型的值和運算元 valarray 相乘。

valarray<Type>& operator*=(const valarray<Type>& right);

valarray<Type>& operator*=(const Type& right);

參數

right
與 valarray 操作數相乘之操作數 valarray 相同之專案型別的 或 值,即操作數 valarray。

傳回值

valarray,其專案是操作數valarray和 的元素明智乘積right。

範例

// valarray_op_emult.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> vaL ( 8 ), vaR ( 8 );
   for ( i = 0 ; i < 8 ; i += 2 )
      vaL [ i ] =  2;
   for ( i = 1 ; i < 8 ; i += 2 )
      vaL [ i ] =  -1;
   for ( i = 0 ; i < 8 ; i++ )
      vaR [ i ] =  i;

   cout << "The initial valarray is: ( ";
      for ( i = 0 ; i < 8 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;

   cout << "The initial Right valarray is: ( ";
      for ( i = 0 ; i < 8 ; i++ )
         cout << vaR [ i ] << " ";
   cout << ")." << endl;

   vaL *= vaR;
   cout << "The element-by-element result of "
        << "the multiplication is the"
        << endl << "valarray: ( ";
      for ( i = 0 ; i < 8 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;
}
/* Output:
The initial valarray is: ( 2 -1 2 -1 2 -1 2 -1 ).
The initial Right valarray is: ( 0 1 2 3 4 5 6 7 ).
The element-by-element result of the multiplication is the
valarray: ( 0 -1 4 -3 8 -5 12 -7 ).
*/

operator+

對 valarray 中每個項目套用加號的一元運算子。 對於一般算術值,此作業沒有任何作用。

valarray<Type> operator+() const;

傳回值

valarray,其專案是操作數位列的一元加結果。

範例

// valarray_op_eplus.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> vaL ( 10 );
   valarray<int> vaPLUS ( 10 );
   for ( i = 0 ; i < 10 ; i += 2 )
      vaL [ i ] =  -i;
   for ( i = 1 ; i < 10 ; i += 2 )
      vaL [ i ] =  i-1;

   cout << "The initial valarray is:  ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;

   vaPLUS = +vaL;
   cout << "The element-by-element result of "
        << "the operator+ is the"
        << endl << "valarray: ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << vaPLUS [ i ] << " ";
   cout << ")." << endl;
}
The initial valarray is:  ( 0 0 -2 2 -4 4 -6 6 -8 8 ).
The element-by-element result of the operator+ is the
valarray: ( 0 0 -2 2 -4 4 -6 6 -8 8 ).

operator+=

將指定之 valarray 的項目或此項目類型的值加到運算元 valarray。

valarray<Type>& operator+=(const valarray<Type>& right);

valarray<Type>& operator+=(const Type& right);

參數

right
專案 valarray 型別的 或 值,與要加入操作數的操作數 valarray 相同,專案型別為 操作數 valarray。

傳回值

valarray,其專案是操作數valarray和的專案明智總和right。

範例

// valarray_op_eadd.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> vaL ( 8 ), vaR ( 8 );
   for ( i = 0 ; i < 8 ; i += 2 )
      vaL [ i ] =  2;
   for ( i = 1 ; i < 8 ; i += 2 )
      vaL [ i ] =  -1;
   for ( i = 0 ; i < 8 ; i++ )
      vaR [ i ] =  i;

   cout << "The initial valarray is: ( ";
      for (i = 0 ; i < 8 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;

   cout << "The initial  right valarray is: ( ";
      for (i = 0 ; i < 8 ; i++ )
         cout << vaR [ i ] << " ";
   cout << ")." << endl;

   vaL += vaR;
   cout << "The element-by-element result of "
        << "the sum is the"
        << endl << "valarray: ( ";
      for (i = 0 ; i < 8 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;
}
The initial valarray is: ( 2 -1 2 -1 2 -1 2 -1 ).
The initial  right valarray is: ( 0 1 2 3 4 5 6 7 ).
The element-by-element result of the sum is the
valarray: ( 2 0 4 2 6 4 8 6 ).

operator-

將算術否定套用至 中 valarray每個專案的一元運算元。

valarray<Type> operator-() const;

傳回值

, valarray 其專案會以算術方式從操作數 valarray的專案否定。

範例

// valarray_op_eminus.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> vaL ( 10 );
   valarray<int> vaMINUS ( 10 );
   for ( i = 0 ; i < 10 ; i += 2 )
      vaL [ i ] =  -i;
   for ( i = 1 ; i < 10 ; i += 2 )
      vaL [ i ] =  i-1;

   cout << "The initial valarray is:  ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;

   vaMINUS = -vaL;
   cout << "The element-by-element result of "
        << "the operator- is the"
        << endl << "valarray: ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << vaMINUS [ i ] << " ";
   cout << ")." << endl;
}
The initial valarray is:  ( 0 0 -2 2 -4 4 -6 6 -8 8 ).
The element-by-element result of the operator- is the
valarray: ( 0 0 2 -2 4 -4 6 -6 8 -8 ).

operator-=

將運算元 valarray 減去指定之 valarray 的項目或此項目類型的值。

valarray<Type>& operator-=(const valarray<Type>& right);

valarray<Type>& operator-=(const Type& right);

參數

right
專案 valarray 型別的 或 值,與要從操作數 減去之操作 valarray 數的操作數 valarray相同。

傳回值

valarray,其專案是操作數valarray和right的專案明智差異。

範例

// valarray_op_esub.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> vaL ( 8 ), vaR ( 8 );
   for ( i = 0 ; i < 8 ; i += 2 )
      vaL [ i ] =  10;
   for ( i = 1 ; i < 8 ; i += 2 )
      vaL [ i ] =  0;
   for ( i = 0 ; i < 8 ; i++ )
      vaR [ i ] =  i;

   cout << "The initial valarray is: ( ";
      for (i = 0 ; i < 8 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;

   cout << "The initial  right valarray is: ( ";
      for ( i = 0 ; i < 8 ; i++ )
         cout << vaR [ i ] << " ";
   cout << ")." << endl;

   vaL -= vaR;
   cout << "The element-by-element result of "
        << "the difference is the"
        << endl << "valarray: ( ";
      for ( i = 0 ; i < 8 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;
}
The initial valarray is: ( 10 0 10 0 10 0 10 0 ).
The initial  right valarray is: ( 0 1 2 3 4 5 6 7 ).
The element-by-element result of the difference is the
valarray: ( 10 -1 8 -3 6 -5 4 -7 ).

operator/=

將運算元 valarray 除以指定之 valarray 的項目或此項目類型的值。

valarray<Type>& operator/=(const valarray<Type>& right);

valarray<Type>& operator/=(const Type& right);

參數

right
與 valarray 操作數相同之專案型別的 或 值,該操作數 valarray 要以元素為根據,將它分割成操作數 valarray。

傳回值

valarray,其專案是操作數valarray除以right的專案商。

範例

// valarray_op_ediv.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<double> vaL ( 6 ), vaR ( 6 );
   for ( i = 0 ; i < 6 ; i += 2 )
      vaL [ i ] =  100;
   for ( i = 1 ; i < 6 ; i += 2 )
      vaL [ i ] =  -100;
   for ( i = 0 ; i < 6 ; i++ )
      vaR [ i ] =  2*i;

   cout << "The initial valarray is: ( ";
      for (i = 0 ; i < 6 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;

   cout << "The initial Right valarray is: ( ";
      for (i = 0 ; i < 6 ; i++ )
         cout << vaR [ i ] << " ";
   cout << ")." << endl;

   vaL /= vaR;
   cout << "The element-by-element result of "
        << "the quotient is the"
        << endl << "valarray: ( ";
      for (i = 0 ; i < 6 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;
}
The initial valarray is: ( 100 -100 100 -100 100 -100 ).
The initial Right valarray is: ( 0 2 4 6 8 10 ).
The element-by-element result of the quotient is the
valarray: ( inf -50 25 -16.6667 12.5 -10 ).

operator=

指派項目至 valarray,其值直接受到指定或指定為其他 valarray 的一部份,或由 slice_array、gslice_array、mask_array 或 indirect_array 指定。

valarray<Type>& operator=(const valarray<Type>& right);

valarray<Type>& operator=(valarray<Type>&& right);

valarray<Type>& operator=(const Type& val);

valarray<Type>& operator=(const slice_array<Type>& slicearray);

valarray<Type>& operator=(const gslice_array<Type>& gslicearray);

valarray<Type>& operator=(const mask_array<Type>& maskarray);

valarray<Type>& operator=(const indirect_array<Type>& indarray);

參數

right
valarray要複製到操作數 valarray。

val
要指派給操作數 valarray專案的值。

slicearray
slice_array要複製到操作數 valarray。

gslicearray
gslice_array要複製到操作數 valarray。

maskarray
mask_array要複製到操作數 valarray。

indarray
indirect_array要複製到操作數 valarray。

傳回值

第一個成員運算子會將受控制序列取代為 right 所控制之序列的複本。

第二個成員運算符與第一個 運算符相同,但具有 Rvalue 參考宣告子: &&。

第三個成員運算符會以 val 複本取代受控制序列的每個元素。

其餘成員運算符會取代其自變數所選取之受控制序列的那些元素,這些元素只會由 operator[]產生。

如果在取代控制序列中的成員值取決於初始控制序列中的成員,則結果為未定義。

備註

如果受控制序列的長度變更,通常結果為未定義。 不過,在此實作中,此效果只會讓任何針對控制序列中項目的指標或參考失效。

範例

// valarray_op_assign.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> va ( 10 ), vaR ( 10 );
   for ( i = 0 ; i < 10 ; i += 1 )
      va [ i ] = i;
   for ( i = 0 ; i < 10 ; i+=1 )
      vaR [ i ] = 10 - i;

   cout << "The operand valarray va is:";
   for ( i = 0 ; i < 10 ; i++ )
      cout << " " << va [ i ];
   cout << endl;

   cout << "The operand valarray vaR is:";
      for ( i = 0 ; i < 10 ; i++ )
         cout << " " << vaR [ i ];
   cout << endl;

   // Assigning vaR to va with the first member function
   va = vaR;
   cout << "The reassigned valarray va is:";
   for ( i = 0 ; i < 10 ; i++ )
      cout << " " << va [ i ];
   cout << endl;

   // Assigning elements of value 10 to va
   // with the second member function
   va = 10;
   cout << "The reassigned valarray va is:";
      for ( i = 0 ; i < 10 ; i++ )
         cout << " " << va [ i ];
   cout << endl;
}
The operand valarray va is: 0 1 2 3 4 5 6 7 8 9
The operand valarray vaR is: 10 9 8 7 6 5 4 3 2 1
The reassigned valarray va is: 10 9 8 7 6 5 4 3 2 1
The reassigned valarray va is: 10 10 10 10 10 10 10 10 10 10

operator[]

傳回項目參考或其指定索引處的值或指定的子集。

Type& operator[](size_t index);

slice_array<Type> operator[](slice slicearray);

gslice_array<Type> operator[](const gslice& gslicearray);

mask_array<Type> operator[](const valarray<bool>& boolarray);

indirect_array<Type> operator[](const valarray<size_t>& indarray);

Type operator[](size_t index) const;

valarray<Type> operator[](slice slicearray) const;

valarray<Type> operator[](const gslice& gslicearray) const;

valarray<Type> operator[](const valarray<bool>& boolarray) const;

valarray<Type> operator[](const valarray<size_t>& indarray) const;

參數

index
要指派值的項目索引。

slicearray
slice_array的 valarray ,指定要選取或傳回至新 valarray的子集。

gslicearray
gslice_array的 valarray ,指定要選取或傳回至新 valarray的子集。

boolarray
bool_array的 valarray ,指定要選取或傳回至新 valarray的子集。

indarray
indirect_array的 valarray ,指定要選取或傳回至新 valarray的子集。

傳回值

項目參考或其指定索引處的值或指定的子集。

備註

成員運算子會多載,以提供數種方式,從所 *this控制的項目中選取元素序列。 五個成員運算子的第一組會與各種多載 operator= 一起運作,以允許選擇性取代受控制序列的 (配量)。 選取的項目必須存在。

使用 定義為 1 或 2 進行 _ITERATOR_DEBUG_LEVEL 編譯時,如果您嘗試存取 超出 界限的專案 valarray,就會發生運行時錯誤。 如需詳細資訊,請參閱已檢查的迭代器 (部分機器翻譯)。

範例

如需如何宣告和使用 operator[]的範例,請參閱 和 gslice::gslice 的範例slice::slice。

operator^=

取得具有指定valarray或專案型別值之的位獨佔 或 ()。^valarray

valarray<Type>& operator^=(const valarray<Type>& right);

valarray<Type>& operator^=(const Type& right);

參數

right
專案 valarray 型別的 或 值,與 valarray 操作數相同,由位互斥或 (XOR) 運算元 (^) 與操作數 valarray結合。

傳回值

valarray,其專案是操作數valarray和right的位互斥或 (XOR)。

備註

位互斥 或 ,稱為 XOR 且以運算符^表示,具有下列語意:指定位b1和 b2b1 ^ b2 ,如果其中一個專案正好是 1,則為 1;如果兩個元素都是 0 或 1,則為 0。

範例

// valarray_op_exor.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
    using namespace std;
    int i;

    valarray<int> vaL ( 10 ), vaR ( 10 );
    for ( i = 0 ; i < 10 ; i += 2 )
        vaL [ i ] =  1;
    for ( i = 1 ; i < 10 ; i += 2 )
        vaL [ i ] =  0;
    for ( i = 0 ; i < 10 ; i += 3 )
        vaR [ i ] =  i;
    for ( i = 1 ; i < 10 ; i += 3 )
        vaR [ i ] =  i-1;
    for ( i = 2 ; i < 10 ; i += 3 )
        vaR [ i ] =  i-1;

    cout << "The initial operand valarray is:  ( ";
        for (i = 0 ; i < 10 ; i++ )
            cout << vaL [ i ] << " ";
    cout << ")." << endl;

    cout << "The  right valarray is: ( ";
        for ( i = 0 ; i < 10 ; i++ )
            cout << vaR [ i ] << " ";
    cout << ")." << endl;

    vaL ^= vaR;
    cout << "The element-by-element result of "
        << "the bitwise XOR operator^= is the"
        << endl << "valarray: ( ";
        for (i = 0 ; i < 10 ; i++ )
            cout << vaL [ i ] << " ";
    cout << ")." << endl;
}
The initial operand valarray is:  ( 1 0 1 0 1 0 1 0 1 0 ).
The  right valarray is: ( 0 0 1 3 3 4 6 6 7 9 ).
The element-by-element result of the bitwise XOR operator^= is the
valarray: ( 1 0 0 3 2 4 7 6 6 9 ).

operator|=

取得陣列中專案的位 OR (|) ,其具有指定 valarray 中對應的專案,或是具有專案類型的值。

valarray<Type>& operator|=(const valarray<Type>& right);

valarray<Type>& operator|=(const Type& right);

參數

right
專案 valarray 型別的 或 值,與 valarray 操作數相同的操作數,由位 OR (|) 與操作數 valarray結合的項目類型。

傳回值

,valarray其專案是 操作數valarray的元素位 OR (|) 由 right。

備註

位運算只能用來操作整數位資料類型中的位,例如 char 和 int。 它不適用於 float、 double、 long double、 void、、 bool或其他更複雜的數據類型。

位 OR (|) 適用於個別位層級上的數據類型。 如果至少有一個位b1b2b1 | b2是 1,則為 1;如果兩個位都是 0,則為 0。

範例

// valarray_class_op_bitor.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> vaL ( 10 ), vaR ( 10 );
   for ( i = 0 ; i < 10 ; i += 2 )
      vaL [ i ] =  1;
   for ( i = 1 ; i < 10 ; i += 2 )
      vaL [ i ] =  0;
   for ( i = 0 ; i < 10 ; i += 3 )
      vaR [ i ] =  i;
   for ( i = 1 ; i < 10 ; i += 3 )
      vaR [ i ] =  i-1;
   for ( i = 2 ; i < 10 ; i += 3 )
      vaR [ i ] =  i-1;

   cout << "The initial operand valarray is:"
        << endl << "( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;

   cout << "The  right valarray is:"
        << endl << "( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << vaR [ i ] << " ";
   cout << ")." << endl;

   vaL |= vaR;
   cout << "The element-by-element result of "
        << "the bitwise OR"
        << endl << "operator|= is the valarray:"
        << endl << "( ";
      for (i = 0 ; i < 10 ; i++ )
         cout << vaL [ i ] << " ";
   cout << ")." << endl;
}
The initial operand valarray is:
( 1 0 1 0 1 0 1 0 1 0 ).
The  right valarray is:
( 0 0 1 3 3 4 6 6 7 9 ).
The element-by-element result of the bitwise OR
operator|= is the valarray:
( 1 0 1 3 3 4 7 6 7 9 ).

operator~

一元運算子,可取得 中 valarray每個元素值的位補碼。

valarray<Type> operator~() const;

傳回值

valarray值,其為操作數 valarray之專案值的一元位補碼。

備註

位運算只能操作整數位別中的位,例如 char 和 int。 它不適用於 float、 double、 long double、 void、、 bool或其他更複雜的數據類型。

一元位補碼運算子 ~ 會套用至個別位層級的數據類型。 指定位 b, ~b 如果 b 為 0,則為 1,如果 為 1,則 b 為 0。

範例

// valarray_op_bitnot.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
    using namespace std;
    int i;

    valarray<unsigned short int> vaL1 ( 10 );
    valarray<unsigned short int> vaNOT1 ( 10 );
    for ( i = 0 ; i < 10 ; i += 2 )
        vaL1 [ i ] =  i;
    for ( i = 1 ; i < 10 ; i += 2 )
        vaL1 [ i ] =  5*i;

    cout << "The initial valarray <unsigned short int> is:  ( ";
        for ( i = 0 ; i < 10 ; i++ )
            cout << vaL1 [ i ] << " ";
    cout << ")." << endl;

    vaNOT1 = ~vaL1;
    cout << "The element-by-element result of "
        << "the bitwise NOT operator~ is the"
        << endl << "valarray: ( ";
        for ( i = 0 ; i < 10 ; i++ )
            cout << vaNOT1 [ i ] << " ";
    cout << ")." << endl << endl;

    valarray<int> vaL2 ( 10 );
    valarray<int> vaNOT2 ( 10 );
    for ( i = 0 ; i < 10 ; i += 2 )
        vaL2 [ i ] =  i;
    for ( i = 1 ; i < 10 ; i += 2 )
        vaL2 [ i ] =  -2 * i;

    cout << "The initial valarray <int> is:  ( ";
        for ( i = 0 ; i < 10 ; i++ )
            cout << vaL2 [ i ] << " ";
    cout << ")." << endl;

    vaNOT2 = ~vaL2;
    cout << "The element-by-element result of "
        << "the bitwise NOT operator~ is the"
        << endl << "valarray: ( ";
        for ( i = 0 ; i < 10 ; i++ )
            cout << vaNOT2 [ i ] << " ";
    cout << ")." << endl;

    // The negative numbers are represented using
    // the two's complement approach, so adding one
    // to the flipped bits returns the negative elements
    vaNOT2 = vaNOT2 + 1;
    cout << "The element-by-element result of "
        << "adding one"
        << endl << "is the negative of the "
        << "original elements the"
        << endl << "valarray: ( ";
        for ( i = 0 ; i < 10 ; i++ )
            cout << vaNOT2 [ i ] << " ";
    cout << ")." << endl;
}
The initial valarray <unsigned short int> is:  ( 0 5 2 15 4 25 6 35 8 45 ).
The element-by-element result of the bitwise NOT operator~ is the
valarray: ( 65535 65530 65533 65520 65531 65510 65529 65500 65527 65490 ).

The initial valarray <int> is:  ( 0 -2 2 -6 4 -10 6 -14 8 -18 ).
The element-by-element result of the bitwise NOT operator~ is the
valarray: ( -1 1 -3 5 -5 9 -7 13 -9 17 ).
The element-by-element result of adding one
is the negative of the original elements the
valarray: ( 0 2 -2 6 -4 10 -6 14 -8 18 ).

resize

將中的 valarray 項目數目變更為指定的數位。

void resize(
    size_t new_size);

void resize(
    size_t new_size,
    const Type val);

參數

new_size
重設大小的 valarray元素數目。

val
要提供給已重設大小的 valarray專案的值。

備註

第一個成員函式會使用其預設建構函式來初始化項目。

控制序列中項目的任何指標或參考都會失效。

範例

下列範例示範如何使用 valarray::resize 成員函式。

// valarray_resize.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main()
{
    using namespace std;
    int i;
    size_t size1, sizeNew;

    valarray<int> va1(10);
    for (i = 0; i < 10; i+=1)
        va1[i] = i;

    cout << "The valarray contains ( ";
        for (i = 0; i < 10; i++)
            cout << va1[i] << " ";
    cout << ")." << endl;

    size1 = va1.size();
    cout << "The number of elements in the valarray is: "
         << size1  << "." <<endl << endl;

    va1.resize(15, 10);

    cout << "The valarray contains ( ";
        for (i = 0; i < 15; i++)
            cout << va1[i] << " ";
    cout << ")." << endl;
    sizeNew = va1.size();
    cout << "The number of elements in the resized valarray is: "
         << sizeNew  << "." <<endl << endl;
}
The valarray contains ( 0 1 2 3 4 5 6 7 8 9 ).
The number of elements in the valarray is: 10.

The valarray contains ( 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 ).
The number of elements in the resized valarray is: 15.

shift

依指定的位置數目,將 中的所有 valarray 項目移位。

valarray<Type> shift(int count) const;

參數

count
項目要向前移位的位置數目。

傳回值

新的 valarray ,與操作valarray數中的位置相較之下,所有元素都已移至 前面valarray,或向左移動count。

備註

正值的 count 會將項目左移 count 個位置,並用零填滿。

負值的 count 會將項目右移 count 個位置,並用零填滿。

範例

// valarray_shift.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
   using namespace std;
   int i;

   valarray<int> va1 ( 10 ), va2 ( 10 );
   for ( i = 0 ; i < 10 ; i += 1 )
      va1 [ i ] =  i;
   for ( i = 0 ; i < 10 ; i += 1 )
      va2 [ i ] = 10 - i;

   cout << "The operand valarray va1(10) is: ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << va1 [ i ] << " ";
   cout << ")." << endl;

   // A positive parameter shifts elements left
   va1 = va1.shift ( 4 );
   cout << "The shifted valarray va1 is: va1.shift (4) = ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << va1 [ i ] << " ";
   cout << ")." << endl;

   cout << "The operand valarray va2(10) is: ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << va2 [ i ] << " ";
   cout << ")." << endl;

   // A negative parameter shifts elements right
   va2 = va2.shift ( - 4 );
   cout << "The shifted valarray va2 is: va2.shift (-4) = ( ";
      for ( i = 0 ; i < 10 ; i++ )
         cout << va2 [ i ] << " ";
   cout << ")." << endl;
}
The operand valarray va1(10) is: ( 0 1 2 3 4 5 6 7 8 9 ).
The shifted valarray va1 is: va1.shift (4) = ( 4 5 6 7 8 9 0 0 0 0 ).
The operand valarray va2(10) is: ( 10 9 8 7 6 5 4 3 2 1 ).
The shifted valarray va2 is: va2.shift (-4) = ( 0 0 0 0 10 9 8 7 6 5 ).

size

尋找 valarray 中的項目數目。

size_t size() const;

傳回值

操作數 valarray中的項目數目。

範例

下列範例示範如何使用 valarray::size 成員函式。

// valarray_size.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main()
{
    using namespace std;
    int i;
    size_t size1, size2;

    valarray<int> va1(10), va2(12);
    for (i = 0; i < 10; i += 1)
        va1[i] =  i;
    for (i = 0; i < 10; i += 1)
        va2[i] =  i;

    cout << "The operand valarray va1(10) is: ( ";
        for (i = 0; i < 10; i++)
            cout << va1[i] << " ";
    cout << ")." << endl;

    size1 = va1.size();
    cout << "The number of elements in the valarray va1 is: va1.size = "
         << size1  << "." <<endl << endl;

    cout << "The operand valarray va2(12) is: ( ";
        for (i = 0; i < 10; i++)
            cout << va2[i] << " ";
    cout << ")." << endl;

    size2 = va2.size();
    cout << "The number of elements in the valarray va2 is: va2.size = "
         << size2  << "." << endl << endl;

    // Initializing two more elements to va2
    va2[10] = 10;
    va2[11] = 11;
    cout << "After initializing two more elements,\n"
         << "the operand valarray va2(12) is now: ( ";
        for (i = 0; i < 12; i++)
            cout << va2[i] << " ";
    cout << ")." << endl;
    cout << "The number of elements in the valarray va2 is still: "
         << size2  << "." << endl;
}
The operand valarray va1(10) is: ( 0 1 2 3 4 5 6 7 8 9 ).
The number of elements in the valarray va1 is: va1.size = 10.

The operand valarray va2(12) is: ( 0 1 2 3 4 5 6 7 8 9 ).
The number of elements in the valarray va2 is: va2.size = 12.

After initializing two more elements,
the operand valarray va2(12) is now: ( 0 1 2 3 4 5 6 7 8 9 10 11 ).
The number of elements in the valarray va2 is still: 12.

sum

判斷非零長度的 valarray 中所有項目的總和。

Type sum() const;

傳回值

操作數 valarray的專案總和。

備註

如果長度大於一,則成員函式會藉由在 類別Type的元素組之間套用operator+=值,將值新增至總和。 這表示必須為類型的 Type專案提供運算元。

範例

// valarray_sum.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
    using namespace std;
    int i;
    int sumva = 0;

    valarray<int> va ( 10 );
    for ( i = 0 ; i < 10 ; i+=1 )
        va [ i ] =  i;

    cout << "The operand valarray va (10) is: ( ";
        for ( i = 0 ; i < 10 ; i++ )
            cout << va [ i ] << " ";
    cout << ")." << endl;

    sumva = va.sum ( );
    cout << "The sum of elements in the valarray is: "
        << sumva  << "." <<endl;
}
The operand valarray va (10) is: ( 0 1 2 3 4 5 6 7 8 9 ).
The sum of elements in the valarray is: 45.

swap

交換兩個 valarray 物件的項目。

void swap(valarray& right);

參數

right
valarray,提供要交換的項目。

備註

成員函式會交換 *this 和 right 之間受控制的序列。 它會在常數時間執行此作業,不會擲回任何例外狀況,而且不會使兩個受控制序列中元素的參考、指標或反覆運算器失效。

valarray

建構具有特定大小或具有特定值之項目的 valarray,或建構為另一個 valarray 的複本或另一個 valarray 的子集。

valarray();

explicit valarray(
    size_t count);

valarray(
    const Type& val,
    size_t count);

valarray(
    const Type* ptr,
    size_t count);

valarray(
    const valarray<Type>& right);

valarray(
    const slice_array<Type>& sliceArray);

valarray(
    const gslice_array<Type>& gsliceArray);

valarray(
    const mask_array<Type>& maskArray);

valarray(
    const indirect_array<Type>& indArray);

valarray(
    valarray<Type>&& tight);

valarray(
    initializer_list<Type> iList);

參數

count
要位於中的 valarray項目數目。

val
值,用於初始化 中的 valarray專案。

ptr
要用來初始化 中 valarray專案之值的指標。

right
要初始化新 之的現有 valarray valarray。

sliceArray
slice_array,其專案值要用於初始化所建構之 專案的 valarray 。

gsliceArray
gslice_array,其專案值要用於初始化所建構之 專案的 valarray 。

maskArray
mask_array,其專案值要用於初始化所建構之 專案的 valarray 。

indArray
indirect_array,其專案值要用於初始化所建構之 專案的 valarray 。

iList
initializer_list,包含要複製的專案。

備註

第一個 (預設) 建構函式會將 物件初始化為空 valarray的 。 接下來的三個建構函式會將 物件初始化為 valarray 專案的 count ,如下所示:

  • 如果是明確 valarray(size_t count),以預設建構函式初始化每個項目。

  • 對於 valarray(const Type& val, count),以 val 初始化每個項目。

  • 針對 valarray(const Type* ptr, count),位置 I 的專案會使用 ptr[I]初始化。

每個剩餘的建構函式會將 物件初始化為 valarray<Type> 自變數中所指定子集所決定的物件。

最後一個建構函式與最後一個 建構函式相同,但具有 Rvalue 參考宣告子: &&。

範例

// valarray_ctor.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main()
{
    using namespace std;
    int i;

    // The second member function
    valarray<int> va(10);
    for (auto i : va){
        va[i] = 2 * (i + 1);
    }

    cout << "The operand valarray va is:\n(";
    for (auto i : va) {
        cout << " " << va[i];
    }
    cout << " )" << endl;

    slice Slice(2, 4, 3);

    // The fifth member function
    valarray<int> vaSlice = va[Slice];

    cout << "The new valarray initialized from the slice is vaSlice =\n"
        << "va[slice( 2, 4, 3)] = (";
    for (int i = 0; i < 3; i++) {
        cout << " " << vaSlice[i];
    }
    cout << " )" << endl;

    valarray<int> va2{{ 1, 2, 3, 4 }};
    for (auto& v : va2) {
        cout << v << " ";
    }
    cout << endl;
}
The operand valarray va is:
( 0 2 2 2 2 2 2 2 2 2 )
The new valarray initialized from the slice is vaSlice =
va[slice( 2, 4, 3)] = ( 0 0 0 )
1 2 3 4

value_type

代表儲存在 valarray 中之項目類型的類型。

typedef Type value_type;

備註

此類型是範本參數 Type的同義字。

範例

// valarray_value_type.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
    using namespace std;
    int i;
    valarray<int> va ( 10 );
    for ( i = 0 ; i < 10 ; i += 2 )
        va [ i ] =  i;
    for ( i = 1 ; i < 10 ; i += 2 )
        va [ i ] =  -1;

    cout << "The initial operand valarray is:  ( ";
        for ( i = 0 ; i < 10 ; i++ )
            cout << va [ i ] << " ";
    cout << ")." << endl;

    // value_type declaration and initialization:
    valarray<int>::value_type Right = 10;

    cout << "The declared value_type Right is: "
            << Right << endl;
    va *= Right;
    cout << "The resulting valarray is:  ( ";
        for ( i = 0 ; i < 10 ; i++ )
            cout << va [ i ] << " ";
    cout << ")." << endl;
}
The initial operand valarray is:  ( 0 -1 2 -1 4 -1 6 -1 8 -1 ).
The declared value_type Right is: 10
The resulting valarray is:  ( 0 -10 20 -10 40 -10 60 -10 80 -10 ).

另請參閱

C++標準連結庫中的線程安全性