Forgotten Password?

Or login with:

  • Facebook
  • Google
  • Yahoo


Is like a vector, but is optimized to get good performance for the processing of value arrays


The valarray template is defined in the standard header <valarray>, and in the nonstandard backward-compatibility header <valarray.h>.
namespace std {
       template <class T>
       class valarray;


A valarray is a representation of the mathematical concept of a linear sequence of values.

It is like a vector but is designed for high speed numerics at the expense of some programming ease and general purpose use.

A simple example of creating a valarray is:
std::valarray<int> val1(10);              // valarray of ten ints with value 0
std::valarray<float> val2(8.3, 10);       // valarray of ten floats with value 8.3


Valarray has many features that make it ideally suited for use with vector processors in traditional vector supercomputers and SIMD units in consumer-level scalar processors, and also ease vector mathematics programming even in scalar computers.

Valarray Operations

Create, Copy and Destroy Operations

Operation Effect
valarray() Default constructor, creates an empty valarray
valarray(size_t n) Creates a valarray that contains n elements
valarray(const T& val,size_t n) Creates a valarray with n elements initialized by val
valarray(const T* a, size_t n) Creates a valarray with n elements initialized by the values of the elements in array a
valarray(const valarray& va) Copy constructor
~valarray() Destroys all elements and frees the memory

Assignment Operations

Operation Effect
operator =(const valarray& va) Assigns the elements of the valarray va
operator =(const T& value) Assigns value to each element of the valarray

Member Functions

Operation Effect
size() const Returns the actual number of elements
resize(size_t n) Change the size of the valarray to n
min() const Returns the minimum value of all elements
max() const Returns the maximum value of all elements
sum() const Returns the sum of all elements
shift(int n) const Returns a new valarray in which all elements are shifted by n positions
cshift(int n) const Returns a new valarray in which all elements are shifted cyclically by n positions
apply(T op (T)) const Returns a new valarray with all elements processed by op()

Element Access

Operation Effect
operator [ ](size_t idx) Return the valarray element that has index idx

Transcendental Functions

Operation Effect
abs Absolute value of valarray elements
acos Arc cosine of valarray elements
asin Arc sine of valarray elements
atan Arc tangent of valarray elements
atan2 Atan2 of valarray elements
cos Cosine of valarray elements
cosh Hyperbolic cosine of valarray elements
exp Exponential of valarray elements
log Natural logarithm of valarray elements
log10 Common logarithm of valarray elements
pow Power of valarray elements
sin Sine of valarray elements
sinh Hyperbolic sine of valarray elements
sqrt Square root of valarray elements
tan Tangent of valarray elements
tanh Hyperbolic tangent of valarray elements

The header also overloads operators for the valarray class.


  • Nicolai M. Josuttis: "The C++ Standard Library"

Example - Valarray
The following program illustrates a simple use of valarrays.
#include <iostream>
#include <valarray>
using namespace std;
// print valarray
template <class T>
void printValarray (const valarray<T>& va)
   for (int i=0; i<va.size(); i++) 
     cout << va[i] << ' ';
   cout << endl;
int main()
  // define two valarrays with ten elements
  valarray<double> va1(10), va2(10);
  // assign values 0.0, 1.1, up to 9.9 to the first valarray
  for (int i=0; i<10; i++) 
     va1[i] = i * 1.1;
  // assign -1 to all elements of the second valarray
  va2 = -1;
  // print both valarrays
  // print minimum, maximum, and sum of the first valarray
  cout << "min(): " << val.min() << endl;
  cout << "max(): " << val.max() << endl;
  cout << "sum(): " << val.sum() << endl;
  // assign values of the first to the second valarray
  va2 = va1;
  // remove all elements of the first valarray
  va1.resize (0);
  // print both valarrays again

0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
max(): 9.9
sum(): 49.5
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9

Last Modified: 8 Apr 12 @ 11:53     Page Rendered: 2022-03-14 17:25:11