resize

Syntax:

    #include <valarray>
    void resize( size_t n, const TYPE& val=TYPE() );

The resize() function resizes the number of elements that can be placed into the valarray. The intent is not to create a dynamically resizeable array as the contents may likely be destroyed.

This function may destroy the previous contents of the valarray. The resulting value of the elements is undefined when val is not specified.

The resize() function is typically used to resize the array prior to initialization. Once the values in the valarray are initialized, it is expected that the valarray size will remain the same.

The following code will resize the array, initializing with a contant value:

   #include <valarray>
   #include <iostream>
 
   const int vd[] = {1,2,3,4,5};
   std::valarray<int> v1(vd,5);
   v1.resize(3,1);
   for (size_t i=0; i<v1.size(); ++i) std::cout << v1[i] << std::endl;

Will produce this output:

   111