std::valarray - cppreference.com

From cppreference.com

template<classT>classvalarray;std::valarray is the class for representing and manipulating arrays of values. It supports element-wise mathematical operations and various forms of generalized subscript operators, slicing and indirect access.

Template parameters

T - the type of the elements. The type must meet the

NumericType

requirements Member types

Member functions

(constructor)

constructs new numeric array
(public member function)

[edit]

(destructor)

destructs the numeric array
(public member function)

[edit]

operator=

assigns the contents
(public member function)

[edit]

operator[]

get/set valarray element, slice, or mask
(public member function)

[edit]

operator+operator-operator~operator!

applies a unary arithmetic operator to each element of the valarray
(public member function)

[edit]

operator+=operator-=operator*=operator/=operator%=operator&=operator|=operator^=operator<<=operator>>=

applies compound assignment operator to each element of the valarray
(public member function)

[edit]

swap

swaps with another valarray
(public member function)

[edit]

size

returns the size of valarray
(public member function)

[edit]

resize

changes the size of valarray
(public member function)

[edit]

sum

calculates the sum of all elements
(public member function)

[edit]

min

returns the smallest element
(public member function)

[edit]

max

returns the largest element
(public member function)

[edit]

shift

zero-filling shift the elements of the valarray
(public member function)

[edit]

cshift

circular shift of the elements of the valarray
(public member function)

[edit]

apply

applies a function to every element of a valarray
(public member function)

[edit]

begin

(C++11)

obtains the beginning iterator of valarray
(public member function)

[edit]

end

(C++11)

obtains the past-the-end iterator of valarray
(public member function)

[edit]

Non-member functions

std::swap(std::valarray)

(C++11)

specializes the

std::swap

algorithm
(function template)

[edit]

operator+operator-operator*operator/operator%operator&operator|operator^operator<<operator>>operator&&operator

applies binary operators to each element of two valarrays, or a valarray and a value
(function template)

[edit]

operator==operator!=operator<operator<=operator>operator>=

compares two valarrays or a valarray with a value
(function template)

[edit]

abs(std::valarray)

applies the function abs to each element of valarray
(function template)

[edit]

Exponential functions

exp(std::valarray)

applies the function

std::exp

to each element of valarray
(function template)

[edit]

log(std::valarray)

applies the function

std::log

to each element of valarray
(function template)

[edit]

log10(std::valarray)

applies the function

std::log10

to each element of valarray
(function template)

[edit]

Power functions

pow(std::valarray)

applies the function

std::pow

to two valarrays or a valarray and a value
(function template)

[edit]

sqrt(std::valarray)

applies the function

std::sqrt

to each element of valarray
(function template)

[edit]

Trigonometric functions

sin(std::valarray)

applies the function

std::sin

to each element of valarray
(function template)

[edit]

cos(std::valarray)

applies the function

std::cos

to each element of valarray
(function template)

[edit]

tan(std::valarray)

applies the function

std::tan

to each element of valarray
(function template)

[edit]

asin(std::valarray)

applies the function

std::asin

to each element of valarray
(function template)

[edit]

acos(std::valarray)

applies the function

std::acos

to each element of valarray
(function template)

[edit]

atan(std::valarray)

applies the function

std::atan

to each element of valarray
(function template)

[edit]

atan2(std::valarray)

applies the function

std::atan2

to a valarray and a value
(function template)

[edit]

Hyperbolic functions

sinh(std::valarray)

applies the function

std::sinh

to each element of valarray
(function template)

[edit]

cosh(std::valarray)

applies the function

std::cosh

to each element of valarray
(function template)

[edit]

tanh(std::valarray)

applies the function

std::tanh

to each element of valarray
(function template)

[edit]

Helper classes

slice

BLAS-like slice of a valarray: starting index, length, stride
(class)

[edit]

slice_array

proxy to a subset of a valarray after applying a slice
(class template)

[edit]

gslice

generalized slice of a valarray: starting index, set of lengths, set of strides
(class)

[edit]

gslice_array

proxy to a subset of a valarray after applying a gslice
(class template)

[edit]

mask_array

proxy to a subset of a valarray after applying a boolean mask operator[]
(class template)

[edit]

indirect_array

proxy to a subset of a valarray after applying indirect operator[]
(class template)

[edit]

Deduction guides

(since C++17)

Notes

std::valarray and helper classes are defined to be free of certain forms of aliasing, thus allowing operations on these classes to be optimized similar to the effect of the keyword

restrict

in the C programming language. In addition, functions and operators that take valarray arguments are allowed to return proxy objects to make it possible for the compiler to optimize an expression such as v1=a*v2+v3; as a single loop that executes v1[i]=a*v2[i]+v3[i]; avoiding any temporaries or multiple passes. However,

expression templates

make the same optimization technique available for any C++ container, and the majority of numeric libraries prefer expression templates to valarrays for flexibility. Some C++ standard library implementations use expression templates to implement efficient operations on std::valarray (e.g. GNU libstdc++ and LLVM libc++). Only rarely are valarrays optimized any further, as in e.g.

Intel Integrated Performance Primitives

.

Feature-test

macro ValueStdFeature

__cpp_lib_valarray

202511L

(C++26)
(DR11)begin and end member functions, and iterator and const_iterator member types of std::valarray

[1]

As of 2026-05-27, libstdc++ has not treated the modifications of begin and end functions from

P3016R6

as a defect report against C++11, and only applies the changes since C++26.

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

P3016R6

C++11 1) valarray lacked member types iterator and const_iterator
2) valarray had inconsistent non-member begin() and end() functions 1) member types added
2) changed to member functions See also

(C++26)

data-parallel vector type
(class template)

[edit]

(C++26)

convenience alias template for basic_vec that can specify its width
(alias template)

[edit]

(C++26)

data-parallel type with the element type bool
(class template)

[edit]

(C++26)

convenience alias template for basic_mask that can specify its width
(alias template)

[edit]