好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

valarray详解.doc

3页
  • 卖家[上传人]:hs****ma
  • 文档编号:458006085
  • 上传时间:2022-08-22
  • 文档格式:DOC
  • 文档大小:50.50KB
  • / 3 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • valarray详解C/C++ 2009-04-14 14:37:09 阅读253 评论0   字号:大中小 订阅 还记得vector怎么使用吗?如果想对两个vector进行一些运算,如第一个vector的对象元素加上第二个vector的对应元素,还得写个for循环,或调用相应的库函数,比较麻烦,能不能直接v1 += v2呢?valarray就可以这样写applyApplies a specified function to each element of a valarray.cshiftCyclically shifts all the elements in a valarray by a specified number of positions.maxFinds the largest element in a valarray.minFinds the smallest element in a valarray.resizeChanges the number of elements in a valarray to a specified number, adding or removing elements as required.shiftShifts all the elements in a valarray by a specified number of positions.sizeFinds the number of elements in a valarray.sumDetermines 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 ); //跟上面的那个的区别是,移动后的位置填充为0The 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); //每个都赋值为10val.resize(15);。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.