Assume you need to find the sum of 100 integers. You can create an array or use some STL containers, fill the values and by iterating you can find the sum. Instead of that you can use valarray container for storing any value type and do a number of mathematical operations for all items in just a function call.


See the code snippet for how to use valarray.

// Integer array of 10 members.
valarray<int> IntegerArray( 10 );

// Filling the array.
for( int nIndex = 0; nIndex < 10; ++nIndex )
{
    IntegerArray[ nIndex ] = nIndex;
}

// I need to get the sum of all number.
int nSum = IntegerArray.sum();

If you want to manipulate bulk amount of numbers, better use valarray.


The valarray contains only sum() and misc functions are its member function. Still they are useful, if you want to keep bulk amount of numerals.


Targeted Audience – Beginners.