Simple

There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult. C.A.R. Hoare

valarray

Language C++ | Level Intermediate | Category Standard Template Library (STL) | 2015-11-16 22:04:49

Definition

Valarray hold the array of elements which used in mathematical operations.

Character

 A valarray object is designed to hold an array of values, and easily perform mathematical operations. Represent and manipulate array of values. Valarray is a dynamic data structure and supports variable length of items. Valarray has an interface to retrieve slices of the array. Support optimizations techniques known as expression templates.

source Code

```
#include "stdafx.h"

#include < iostream >
#include < valarray >
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
valarray < int > valarray(10);

cout << "Store items from 0 to 10 in the valarray" << endl;
for (int i = 0; i < 10; i++)
{
valarray[i] = i;
}

cout << "Print items from valarray: ";
for (int i = 0; i < 10; i++)
{
cout << valarray[i] << " ";
}

cout << endl<< "min() of valarray itmes: " << valarray.min() << endl;
cout << "max() of valarray itmes: " << valarray.max() << endl;
cout << "sum() of valarray itmes: " << valarray.sum() << endl;

cout << endl;

return 0;
}

```

Output

```
Store items from 0 to 10 in the valarray
Print items from valarray: 0 1 2 3 4 5 6 7 8 9
min() of valarray itmes: 0
max() of valarray itmes: 9
sum() of valarray itmes: 45

```

When to use

The valarray array classes are defined to be free of certain forms of aliasing, thus allowing operations on these classes to be optimized.