C++ Standard Library C++ STL Library

C++ <valarray> - size() Function



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

Syntax

size_t size() const;

Parameters

No parameter is required.

Return Value

Returns the size of the valarray.

Time Complexity

Depends on library implementation, but likely constant.

Example:

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: "
      <<varr.size()<<"\n";

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

  //resizing the valarray
  varr.resize(10);
  cout<<"After resizing, size of varr: "
      <<varr.size()<<"\n"; 

  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