std::valarray<T>::operator[]
| (1) | ||
T operator[]( std::size_t pos ) const; | (until C++11) | |
const T& operator[]( std::size_t pos ) const; | (since C++11) | |
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 valarray<bool>& boolarr ) const; | (7) | |
std::mask_array<T> operator[]( const valarray<bool>& boolarr ); | (8) | |
std::valarray<T> operator[]( const valarray<std::size_t>& indarr ) const; | (9) | |
std::indirect_array<T> operator[]( const valarray<std::size_t>& indarr ); | (10) |
Retrieve 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.
Parameters
Return value
std::valarray object containing copies of the selected itemsExceptions
(none).
Precondition
The selected elements must exist.
Notes
- For proper values of
iandj, the following properties are true:
(a[i] = q, a[i]) == q For a non-const a.&a[i+j] == &a[i] + j This means that valarray elements are adjacent in memory.&a[i] != &b[j] - This holds for every objects
aandbthat 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
constmember functions ofstd::valarrayare provided. -
std::valarray,std::slice_array,std::gslice_array,std::mask_arrayandstd::indirect_arraycan be constructed from the replacement type. - All functions accepting an argument of type
const std::valarray&exceptbegin()andend()(since C++11) should also accept the replacement type. - All functions accepting two arguments of type
const std::valarray&should accept every combination ofconst std::valarray&and the replacement type. - The return type does not add more than two levels of template nesting over the most deeply-nested argument type.
Notes
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 <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 << 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 << 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
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/numeric/valarray/operator_at