 st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray<T>8b7c:f320:99b9:690f:4595:cd17:293a:c069operator[]    (1)     T                      operator[]( st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t pos ) const;
  (until C++11)     const T&               operator[]( st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t pos ) const;
  (since C++11)     T&                     operator[]( st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t pos );
  (2)       st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray<T>       operator[]( st8b7c:f320:99b9:690f:4595:cd17:293a:c069slice slicearr ) const;
  (3)       st8b7c:f320:99b9:690f:4595:cd17:293a:c069slice_array<T>    operator[]( st8b7c:f320:99b9:690f:4595:cd17:293a:c069slice slicearr );
  (4)       st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray<T>       operator[]( const st8b7c:f320:99b9:690f:4595:cd17:293a:c069gslice& gslicearr ) const;
  (5)       st8b7c:f320:99b9:690f:4595:cd17:293a:c069gslice_array<T>   operator[]( const st8b7c:f320:99b9:690f:4595:cd17:293a:c069gslice& gslicearr );
  (6)       st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray<T>       operator[]( const valarray<bool>& boolarr ) const;
  (7)       st8b7c:f320:99b9:690f:4595:cd17:293a:c069mask_array<T>     operator[]( const valarray<bool>& boolarr );
  (8)       st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray<T>       operator[]( const valarray<st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t>& indarr ) const;
  (9)       st8b7c:f320:99b9:690f:4595:cd17:293a:c069indirect_array<T> operator[]( const valarray<st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t>& indarr );
  (10)      Retrieve single elements or portions of the array.
The const overloads that return element sequences create a new st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray object. The non-const overloads return classes holding references to the array elements.
 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 st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray object 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.
 Precondition The selected elements must exist.
 Notes  For proper values of i and j, the following properties are true:  
1) (a[i] = q, a[i]) == q  For a non-const a. 
2) &a[i+j] == &a[i] + j This means that valarray elements are adjacent in memory. 
3) &a[i] != &b[j] 
This holds for every objects a and b that 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 st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray. In this case, the replacement type has the following properties:

 All const member functions of st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray are provided. 
 st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray, st8b7c:f320:99b9:690f:4595:cd17:293a:c069slice_array, st8b7c:f320:99b9:690f:4595:cd17:293a:c069gslice_array, st8b7c:f320:99b9:690f:4595:cd17:293a:c069mask_array and st8b7c:f320:99b9:690f:4595:cd17:293a:c069indirect_array can be constructed from the replacement type. 
 All functions accepting an argument of type const st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray& except begin() and end() (since C++11) should also accept the replacement type. 
 All functions accepting two arguments of type const st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray& should accept every combination of const st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray& and the replacement type. 
 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][st8b7c:f320:99b9:690f:4595:cd17:293a:c069slice(0,5,2)] = x; is an error because st8b7c:f320:99b9:690f:4595:cd17:293a:c069mask_array (the type of v[v==n]) does not have operator[].
 Example  #include <iostream>
#include <valarray>
 
int main() 
{
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray<int> data = {0,1,2,3,4,5,6,7,8,9};
 
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << "Initial valarray: ";
    for(int n: data) st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << n << ' ';
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << '\n';
 
    data[data > 5] = -1; // valarray<bool> overload of operator[]
    // the type of data>5 is st8b7c:f320:99b9:690f:4595:cd17:293a:c069valarray<bool>
    // the type of data[data>5] is st8b7c:f320:99b9:690f:4595:cd17:293a:c069mask_array<int>
 
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << "After v[v>5]=-1:  ";
    for(st8b7c:f320:99b9:690f:4595:cd17:293a:c069size_t n = 0; n < data.size(); ++n) 
      st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << data[n] << ' ';  // regular operator[]
    st8b7c:f320:99b9:690f:4595:cd17:293a:c069cout << '\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  

    
{'path': 'public/docs/cpp/numeric/valarray/operator_at.html', 'source': 'DevDocs.IO'}