std::valarray::operator[](3)
π std::valarray::operator[](3) π
Okay bestie, welcome to the page for std::valarray::operator[](3) from the C++ Standard Libary manual β¨ This one is all about grabbing single elements or whole cute little portions of a valarray, and yes, there are ten overloads β we love a main character with range π
β¨ NAME β¨
std::valarray::operator[] β std::valarray::operator[]
πΈ Synopsis πΈ
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[]( const std::gslice& gslicearr )
const; (5)
std::gslice_array<T> operator[]( const std::gslice& gslicearr );
(6)
std::valarray<T> operator[]( const std::valarray<bool>& boolarr
) const; (7)
std::mask_array<T> operator[]( const std::valarray<bool>& boolarr
); (8)
std::valarray<T> operator[]( const std::valarray<std::size_t>&
indarr ) (9)
const;
std::indirect_array<T> operator[]( const std::valarray<std::size_t>&
indarr ); (10)
So whatβs the vibe? Youβre retrieving single elements or portions of the array π«Ά
The const overloads that return element sequences create a new std::valarray object. The non-const overloads return classes holding references to the array elements.
The selected element(s) must exist β no cap, that part is serious:
- for overloads (1,2), if
posis not less thansize(), the behavior is undefined; - and for overloads (3-10), if the argument does not specify a valid subset of
*this, the behavior is undefined.
π Parameters π
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.
π Return value π
- 1,2) A reference to the corresponding element.
- 3,5,7,9) A
std::valarrayobject containing copies of the selected items. - 4,6,8,10) The corresponding data structure containing references to the selected items.
π Exceptions π
May throw implementation-defined exceptions.
π₯ Notes π₯
For proper std::valarray values a, b and proper std::size_t values i, j, all of the following expressions always evaluate to true:
(a[i] = q, a[i]) == qfor non-consta&a[i + j] == &a[i] + j- This means that
std::valarrayelements are adjacent in memory.
- This means that
&a[i] != &b[j]for every objectsaandbthat are not aliases of one another- This means that there are no aliases in the elements and this property can be used to perform some kinds of optimization.
References become invalid on resize() or when the array is destructed.
For overloads (3,5,7,9), the function can be implemented with the return type different from std::valarray. In this case, the replacement type has the following properties:
- All const member functions of
std::valarrayare provided. std::valarray,std::slice_array,std::gslice_array,std::mask_arrayandstd::indirect_arraycan be constructed from the replacement type.- For every function taking a
const std::valarray<T>&exceptbegin()andend()(since C++11), identical functions taking the replacement types shall be added; - For every function taking two
const std::valarray<T>&arguments, identical functions taking every combination ofconst std::valarray<T>&and replacement types shall be added. - The return type does not add more than two levels of template nesting over the most deeply-nested argument type.
Slice/mask/indirect index accesses do not chain: v[v == n][std::slice(0, 5, 2)] = x; is an error because std::mask_array (the type of v[v == n]) does not have operator[].
π¦ Example π¦
#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 9
After v[v > 5] = -1: 0 1 2 3 4 5 -1 -1 -1 -1
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- LWG 389, applied to C++98: the return type of overload (1) was
T; corrected toconst T&. - LWG 430, applied to C++98: the behavior was unclear for overloads (3-10) if an invalid subset is specified; the behavior is undefined in this case.
π Hidden category π
- Pages with unreviewed LWG DR marker
π«Ά Cute sign-off: source ishttp://cppreference.com, version/date is2024.06.10, and the page isstd::valarray::operator[](3). Stay iconic, bestie π β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms