Gold mine of Visual C++ tricks!
STL valarray – An easy way to manipulate values.
![]()
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.
| Print article | This entry was posted by Jijo Raj on April 25, 2008 at 4:16 am, and is filed under C++, Codeproject. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |