Common mathematical functions | |||||||||||||||||||||||||||||||
Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray ) | |||||||||||||||||||||||||||||||
Pseudo-random number generation | |||||||||||||||||||||||||||||||
Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
|
const T& operator[](std::size_t pos)const; | (1) | |
T& operator[](std::size_t pos); | (2) | |
std::valarray<T> operator[](std::slice slicearr)const; | (3) | |
std::slice_array<T> operator[](std::slice slicearr); | (4) | |
std::valarray<T> operator[](conststd::gslice& gslicearr)const; | (5) | |
std::gslice_array<T> operator[](conststd::gslice& gslicearr); | (6) | |
std::valarray<T> operator[](conststd::valarray<bool>& boolarr)const; | (7) | |
std::mask_array<T> operator[](conststd::valarray<bool>& boolarr); | (8) | |
std::valarray<T> operator[](conststd::valarray<std::size_t>& indarr)const; | (9) | |
std::indirect_array<T> operator[](conststd::valarray<std::size_t>& indarr); | (10) | |
Retrieve single elements or portions of the array.
Theconst overloads that return element sequences create a newstd::valarray object.The non-const overloads return classes holding references to the array elements.
Ifpos< size() isfalse, the behavior is undefined. | (until C++26) |
Ifpos< size() isfalse:
| (since C++26) |
Contents |
pos | - | position of the element to return |
slicearr | - | slice of the elements to return |
gslicearr | - | gslice of the elements to return |
boolarr | - | mask of the elements to return |
indarr | - | indices of the elements to return |
May throw implementation-defined exceptions.
For properstd::valarray valuesa,b and properstd::size_t valuesi,j, all of the following expressions always evaluate totrue:
References become invalid onresize()
or when the array is destructed.
For overloads(3,5,7,9), The function can be implemented with the return type different fromstd::valarray. In this case, the replacement type has the following properties:
Slice/mask/indirect index accesses do not chain:v[v== n][std::slice(0,5,2)]= x; is an error becausestd::mask_array (the type ofv[v== n]) does not haveoperator[].
#include <cstddef>#include <iomanip>#include <iostream>#include <valarray> int main(){std::valarray<int> data={0,1,2,3,4,5,6,7,8,9}; std::cout<<"Initial valarray: ";for(int n: data)std::cout<<std::setw(3)<< n;std::cout<<'\n'; data[data>5]=-1;// valarray<bool> overload of operator[]// the type of data > 5 is std::valarray<bool>// the type of data[data > 5] is std::mask_array<int> std::cout<<"After v[v > 5] = -1:";for(std::size_t n=0; n< data.size();++n)std::cout<<std::setw(3)<< data[n];// regular operator[]std::cout<<'\n';}
Output:
Initial valarray: 0 1 2 3 4 5 6 7 8 9After v[v > 5] = -1: 0 1 2 3 4 5 -1 -1 -1 -1
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 389 | C++98 | the return type of overload(1) wasT | corrected toconst T& |
LWG 430 | C++98 | the behavior was unclear for overloads (3-10) if an invalid subset is specified | the behavior is undefined in this case |