public member function
<valarray>

std::valarray::resize

void resize (size_t sz, T c = T());
Resize valarray
Resizes the valarray, changing its size to sz elements, and assigns the value c to each element.

After resizing, the previous contents are lost: the valarray will contain sz elements, all of them with a value of c.

All pointers and references to elements of the valarray are invalidated by the call.

Parameters

sz
Size of the valarray, in terms of the number of elements.
size_t is an unsigned integral type.
c
Value to be assigned to each of the elements of the resized array.
T is the template argument of valarray (the value type).

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// valarray::resize example
#include <iostream>     // std::cout
#include <cstddef>      // std::size_t
#include <valarray>     // std::valarray

int increment (int x) {return ++x;}

int main ()
{
  std::valarray<int> myarray (10,5);   // 10 10 10 10 10
  myarray.resize(3);                   // 0  0  0

  std::cout << "myvalarray contains:";
  for (std::size_t n=0; n<myarray.size(); n++)
	  std::cout << ' ' << myarray[n];
  std::cout << '\n';

  return 0;
}

Output:

myvalarray contains: 0 0 0


Complexity

Depends on library implementation (operations may be parallelized).

Iterator validity

Invalidates all iterators, references and sub-arrays of the valarray.

Data races

The valarray and all its elements are modified.

Exception safety

If any operation performed on the elements throws an exception, it causes undefined behavior.
If the function needs to allocate storage and fails, it may throw an exception (such as bad_alloc), although this is not mandated.

See also