C++ valarray Library - Function resize



Description

It resizes the valarray, changing its size to sz elements, and assigns the value c to each element.

Declaration

Following is the declaration for std::valarray::resize function.

void resize (size_t sz, T c = T());

C++11

void resize (size_t sz, T c = T());

Parameters

  • sz − It is used to find size of the valarray.

  • c − It is a value to be assigned to each of the elements of the resized array.

Return Value

none

Exceptions

Basic guarantee − if any operation performed on the elements throws an exception.

Data races

All elements effectively copied are accessed.

Example

In below example explains about std::valarray::resize function.

#include <iostream>
#include <cstddef>
#include <valarray>

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

int main () {
   std::valarray<int> myarray (100,50);
   myarray.resize(3);

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

   return 0;
}

Let us compile and run the above program, this will produce the following result −

myvalarray contains: 0 0 0
valarray.htm
Advertisements