还记得vector怎么使用吗?valarray类似vector,也是一个模板类,其主要被用来对一系列元素进行高速的数字计算,其与vector的主要区别在于以下两点:
1、valarray定义了一组在两个相同长度和相同类型的valarray类对象之间的数字计算,例如xarr = cos(yarr) + sin(zarr);
2、通过重载operater[],可以返回valarray的相关信息(valarray其中某个元素的引用、特定下标的值或者其某个子集)。如果想对两个vector进行一些运算,如第一个vector的对象元素加上第二个vector的对应元素,还得写个for循环,或调用相应的库函数,比较麻烦,能不能直接v1 += v2呢?valarray就可以这样写。
valarray类的公有接口如下:
apply |
Applies a specified function to each element of a valarray. |
cshift |
Cyclically shifts all the elements in a valarray by a specified number of positions. |
max |
Finds the largest element in a valarray. |
min |
Finds the smallest element in a valarray. |
resize |
Changes the number of elements in a valarray to a specified number, adding or removing elements as required. |
shift |
Shifts all the elements in a valarray by a specified number of positions. |
size |
Finds the number of elements in a valarray. |
sum |
Determines the sum of all the elements in a valarray of nonzero length. |
操作符重载:
operator! |
A unary operator that obtains the logical NOT values of each element in a valarray. |
operator%= |
Obtains the remainder of dividing the elements of an array element-wise either by a specified valarrayor by a value of the element type. |
operator&= |
Obtains the bitwise AND of elements in an array either with the corresponding elements in a specifiedvalarray or with a value of the element type. |
operator>>= |
Right-shifts the bits for each element of a valarray operand a specified number of positions or by an element-wise amount specified by a second valarray. |
operator<<= |
Left-shifts the bits for each element of a valarray operand a specified number of positions or by an element-wise amount specified by a second valarray. |
operator*= |
Multiplies the elements of a specified valarray or a value of the element type, element-wise, to an operand valarray. |
operator+ |
A unary operator that applies a plus to each element in a valarray. |
operator+= |
Adds the elements of a specified valarray or a value of the element type, element-wise, to an operandvalarray. |
operator- |
A unary operator that applies a minus to each element in a valarray. |
operator-= |
Subtracts the elements of a specified valarray or a value of the element type, element-wise, from an operand valarray. |
operator/= |
Divides an operand valarray element-wise by the elements of a specified valarray or a value of the element type. |
operator= |
Assigns elements to a valarray whose values are specified either directly or as part of some othervalarray or by a slice_array, gslice_array, mask_array, or indirect_array. |
operator[] |
Returns a reference to an element or its value at specified index or a specified subset. |
operator^= |
Obtains the element-wise exclusive logical or operator (XOR) of an array with either a specified valarray or a value of the element type. |
operator|= |
Obtains the bitwise OR of elements in an array either with the corresponding elements in a specifiedvalarray or with a value of the element type. |
operator~ |
A unary operator that obtains the bitwise NOT values of each element in a valarray. |
ok,可以看出,大部分的操作符都被重载过了,可以直接使用。
现在对一些函数的使用做一个说明:
vaApplied = vaR.apply( MyApplyFunc ); 就是把vaR的每个元素调用了apply参数中的函数,产生新的一个valarray,通过返回值来返回,注意vaApplied一定是有足够空间来存储结果的。
va1 = va1.cshift(4); va2 = va2.cshift(-4); 下面是一个测试结果,相当与一个移动:
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)
va1 = va1.shift ( 4 ); va2 = va2.shift ( - 4 ); //跟上面的那个的区别是,移动后的位置填充为0
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 ).
MaxValue = vaR.max ( ); sumva = va.sum ( ); size1 = va1.size(); va1.resize(15, 10); //每个都赋值为10 val.resize(15);
valarray类构造函数
valarray( );
explicit valarray(size_t _Count);
valarray( const Type& _Val, size_t _Count);
valarray( const Type* _Ptr, size_t _Count);
valarray( const slice_array
valarray( const gslice_array
valarray( const mask_array
valarray( const indirect_array
slice类用法
该类主要配合valarray类使用,可以从valarray中提取子数组
slice( );
slice( size_t _StartIndex,//截取数组的开始位置
const valarray
const valarray
);
用法:
int main( )
{
using namespace std;
int i;
valarray
for ( i = 0 ; i < 20 ; i+=1 )
va [ i ] = 2 * (i + 1 );
cout << "The operand valarray va is:\n( ";
for ( i = 0 ; i < 20 ; i++ )
cout << va [ i ] << " ";
cout << ")." << endl;
slice vaSlice ( 1 , 7 , 3 );
vaResult = va [ vaSlice ];
cout << "\nThe slice of valarray va is vaResult:"
<< "\nva[slice( 1, 7, 3)] = ( ";
for ( i = 0 ; i < 7 ; i++ )
cout << vaResult [ i ] << " ";
cout << ")." << endl;
}
输出结果:
The operand valarray va is:
( 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 ).
The slice of valarray va is vaResult:
va[slice( 1, 7, 3)] = ( 4 10 16 22 28 34 40 ).
Gslice类用法
Gslice类的用法和slice基本相同,只是它截取的是循环子串,当母串进行一次提取后的字串元素数目达不到要求时,gslice会将提取后的母串继续组合进行提取直到满足要求或者母串被提取完了
公共函数(对数组的操作)
1.abs 对数组的每一个元素取绝对值
2.acos 返回每个元素的反余弦值
3.asin 返回每个元素的反正弦值
4.atan 返回每个元素的正切值
5.atan2 笛卡尔正切值
6.cos 余弦值
7.cosh 双曲线余弦值
8.exp 返回自然指数E^x
9.log 返回自然对数
10.log10 返回以10为底的返回自然对数
11.exp 返回x^y
12.sin 正弦值
13.sinh 双曲线正弦值
14.sqrt 开方
15.tan 正切值
16.tanh 反正切
vector 和valarry