valarray类

  还记得vector怎么使用吗?valarray类似vector,也是一个模板类,其主要被用来对一系列元素进行高速的数字计算,其与vector的主要区别在于以下两点 1、valarray定义了一组在两个相同长度和相同类型的valarray类对象之间的数字计算,例如xarr = cos(yarr) + sin(zarr); 2、通过重载operater[],可以返回valarray的相关信息(valarray其中某个元素的引用、特定下标的值或者其某个子集)。如果想对两个vector进行一些运算,如第一个vector的对象元素加上第二个vector的对应元素,还得写个for循环,或调用相应的库函数,比较麻烦,能不能直接v1 += v2呢?

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.

Operators

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 valarray or by a value of the element type.

operator&=

Obtains the bitwise AND of elements in an array either with the corresponding elements in a specified valarray 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 operand valarray.

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 other valarray 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 specified valarray 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<Type>& _SliceArray);

valarray( const gslice_array<Type>& _GsliceArray);

valarray( const mask_array<Type>& _MaskArray);

valarray( const indirect_array<Type>& _IndArray);

 

 

 

slice类用法

该类主要配合valarray类使用,可以从valarray中提取子数组

slice( );

slice( size_t _StartIndex,//截取数组的开始位置

const valarray<size_t> _Len, //子数组的最大长度

const valarray<size_t> _Stride//相隔多少个元素选中一个

);

用法:

int main( )

{

using namespace std;

int i;

 

valarray<int> va ( 20 ), vaResult;

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

C++提供了两个数组模板类:vector 和valarry。这些类是由不同的小组开发的,用于不同的目的。vector模板类是一个容器类和算法系统的一部分,它支持面向容器的操作,如排序、插入、重新排列、搜索、将数据转移到其他的容器中等。而valarray类模板被设计成用于表示数值数组,支持各种数值数组操作,不是STL的一部分。例如它没有push_back()和insert()方法。
 
1.下面程序演示了各自的优势,它使用vector的push_back()方法和自动调整大小功能来收集数据,然后对数组进行排序后,将它们从vector对象复制到一个同样大小的valarray对象中,在执行一些数学运算。
//---- comparing vector and valarray #include <iostream> #include <fstream> #include <valarray> #include <vector> #include <algorithm> #include <cmath> int main() {using namespace std;ifstream fin("E:\testIn.txt",ios_base::in);ofstream fout("E:\testOut.txt",ios_base::out);
vector<double> data;double temp;fout<<"Enter numbers (<=0 to quit):n";while(fin>>temp && temp >0)data.push_back(temp);sort(data.begin(),data.end());int size = data.size();valarray<double> numbers(size);int i;for(i = 0; i < size; i++)numbers[i] = data[i];valarray<double> sq_rts(size);sq_rts = sqrt(numbers);valarray<double> results(size);results = numbers + 2.0 * sq_rts;fout.setf(ios_base::fixed);fout.precision(4);for(i = 0; i < size; i++){fout.width(8);fout<<numbers[i] <<": ";fout.width(8);fout<<results[i]<<endl;}fout<<"done.n";;
fin.close();fout.close();
return 0; }
 
2.下面程序是valarray的扩展下标版本。使用了slice类。lice类对象可用在数组索引,在这种情况下,它代表不是一个值,而是一组值。slice对象被初始化为三个整数值:起始索引、索引数和跨距。
//---- using valarray slices #include <iostream> #include <fstream> #include <valarray> #include <cstdlib> using namespace std;
const int SIZE = 12; typedef std::valarray<int> vint; void show(const vint & v, int cols, ofstream &fout);
int main() {vint valint(SIZE);
ofstream fout("E:\testOut.txt",ios_base::out);
int i;for(i = 0; i< SIZE; i++)valint[i] = rand() % 10;fout<<"Original array:n";show(valint,3,fout);vint vcol(valint[slice(1,4,3)]);fout<<"Second column:n";show(vcol,1,fout);vint vrow(valint[slice(3,3,1)]);fout<<"Second row:n";show(vrow,3,fout);
valint[slice(2,4,3)] = 10;fout<<"Set last column to 10:n";show(valint, 3, fout);
fout<<"Set first column to sum of next two:n";//+ not defined for slices, so convert to valarray<int>valint[slice(0,4,3)] = vint(valint[slice(1,4,3)]) + vint(valint[slice(2,4,3)]);show(valint,3,fout);
return 0; }
void show(const vint & v, int cols, ofstream &fout) {int lim = v.size();for(int i = 0 ; i < lim; i++){fout.width(3);fout<< v[i];if(i % cols == cols - 1)fout<<"n";elsefout<<" ";}if(lim % cols != 0)fout<<"n"; }

 

原文链接: https://www.cnblogs.com/maqiang/archive/2012/05/07/2486768.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    valarray类

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/49680

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午1:20
下一篇 2023年2月9日 上午1:21

相关推荐