C++ Standard Library C++ STL Library

C++ <valarray> - size() Function

The C++ valarray::size function returns the size of the valarray.


size_t size() const;


No parameter is required.

Return Value

Returns the size of the valarray.

Time Complexity

Depends on library implementation, but likely constant.


In the example below, the valarray::size function is used to find out the size of the given valarray.

#include <iostream>
#include <valarray>
using namespace std;
int main (){
  valarray<int> varr;
  cout<<"After construction, size of varr: "

  //assigning the valarray
  varr = valarray<int>(5);
  cout<<"After assignment, size of varr: "

  //resizing the valarray
  cout<<"After resizing, size of varr: "

  return 0;

The output of the above code will be:

After construction, size of varr: 0
After assignment, size of varr: 5
After resizing, size of varr: 10

❮ C++ <valarray> Library