valarray的用法介绍

valarray的用法介绍

#include <iostream>
#include <valarray>
#include <cstdlib>
using namespace std;
template<typename Ty>
valarray<typename Ty::value_type> VA(const Ty& subset)
{
return valarray<typename Ty::value_type>(subset);
}
template<typename Ty>
void printvalarray(const valarray<Ty>& coll,
const char* str = "")
{
int c = 0;
cout << str << "........." << endl;
for(size_t i = 0; i < coll.size(); ++i)
{
if(c!=0 && c%4==0)
cout << endl;
cout << coll[i] << '/t';
++c;
}
cout << endl;
}
int main(void)
{
valarray<double> va(12), vb(12);
for(int i = 0; i < 12; ++i)
{
va[i] = i * 1.5;
vb[i] = i + 10.9;
}
printvalarray(va, "init");
va *= 5.5;
printvalarray(va, "*5.5");
va += vb;
printvalarray(va, "+vb");
printvalarray(VA(va[slice(0, 3, 4)]) , "slice subset");
printvalarray(VA(va[va > 50.5]), "masked subset");
system("pause");
return 0;
}