valarray类似vector,也是一个模板类,其主要被用来对一系列元素进行高速的数字计算,其与vector的主要区别在于以下两点 1、valarray定义了一组在两个相同长度和相同类型的valarray类对象之间的数字计算,例如xarr = cos(yarr) + sin(zarr); 2、通过重载operater[],可以返回valarray的相关信息(valarray其中某个元素的引用、特定下标的值或者其某个子集)。
ms-help://MS.MSDNQTR.v90.chs/dv_vcstdlib/html/5454a934-9536-4371-92c3-c30cb20888ad.htm
apply 通过使用指定的功能到的valarray的每个元素。
cshift 循环移位都在一个的valarray由位置指定数目的元素。
max 查找一个valarray的最大元素。
min 查找一个valarray的最小元素。
resize 改变一个的valarray到一个指定数目的元素的数量,添加或根据需要删除元素。
shift 改变所有的的valarray由位置指定数目的元素。
size 发现在valarray中元素的数量。
sum 确定非零长度的valarray的所有元素的总和。
#include <valarray>
#include <iostream>
//构造函数
void valarrayConstructor(void);
//通过使用指定的功能到的valarray的每个元素
void valarray_apply(void);
//循环移位都在一个的valarray由位置指定数目的元素
void valarray_cshift(void);
//查找一个valarray的最大元素
void valarray_max(void);
//查找一个valarray的最小元素
void valarray_min(void);
//改变一个的valarray到一个指定数目的元素的数量,添加或根据需要删除元素
void valarray_resize(void);
//由位置指定数目的移动在一个valarray的所有元素
void valarray_shift(void);
//发现在valarray中元素的数量
void valarray_size(void);
//确定非零长度的valarray的所有元素的总和
void valarray_sum(void);
int main()
{
//valarrayConstructor();
//valarray_apply();
//valarray_cshift();
//valarray_max();
//valarray_min();
//valarray_resize();
//valarray_shift();
//valarray_size();
valarray_sum();
return 0;
}
//构造函数
void valarrayConstructor(void)
{
using namespace std;
int i;
// The second member function
valarray<int> va(10);
for (i = 0; i < 10; i += 1)
va[i] = 2 * (i + 1);
cout << "The operand valarray va is:\n(";
for (i = 0; i < 10; i++)
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 ="
<< "\nva[slice( 2, 4, 3)] = (";
for (i = 0; i < 3; i++)
cout << " " << vaSlice[i];
cout << " )" << endl;
return;
/*
The operand valarray va is:
( 2 4 6 8 10 12 14 16 18 20 )
The new valarray initialized from the slice is vaSlice =
va[slice( 2, 4, 3)] = ( 6 12 18 )
请按任意键继续. . .
*/
}
int __cdecl MyApplyFunc(int n)
{
return n * 2;
}
//通过使用指定的功能到的valarray的每个元素
void valarray_apply(void)
{
using namespace std;
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;
return;
/*
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 )
请按任意键继续. . .
*/
}
//循环移位都在一个的valarray由位置指定数目的元素
void valarray_cshift(void)
{
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;
return;
/*
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)
请按任意键继续. . .
*/
}
//查找一个valarray的最大元素
void valarray_max(void)
{
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;
return;
/*
The operand valarray is: ( 0 1 8 3 7 5 6 13 2 9 ).
The largest element in the valarray is: 13.
请按任意键继续. . .
*/
}
//改变一个的valarray到一个指定数目的元素的数量,添加或根据需要删除元素
void valarray_resize(void)
{
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;
return ;
/*
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.
请按任意键继续. . .
*/
}
//查找一个valarray的最小元素
void valarray_min(void)
{
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;
return;
/*
The operand valarray is: ( 0 2 3 -3 8 0 -6 14 -3 -9 ).
The smallest element in the valarray is: -9.
请按任意键继续. . .
*/
}
//由位置指定数目的移动在一个valarray的所有元素
void valarray_shift(void)
{
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 ).
请按任意键继续. . .
*/
return;
}
//发现在valarray中元素的数量
void valarray_size(void)
{
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;
return;
/*
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.
请按任意键继续. . .
*/
}
//确定非零长度的valarray的所有元素的总和
void valarray_sum(void)
{
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;
return;
/*
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.
请按任意键继续. . .
*/
}