std::valarray::operator[] - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::valarray::operator[]

From cppreference.com
< cpp‎ | numeric‎ | valarray

| (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.

### [edit] Parameters

| This section is incomplete Reason: Explain the parameters

### [edit] Return value

1,2) A reference to the corresponding element
3,5,7,9) A std::valarray object containing copies of the selected items
4,6,8,10) The corresponding data structure containing references to the selected items

### [edit] Exceptions

(none)

### [edit] Precondition

The selected elements must exist.

### [edit] 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 std::valarray. In this case, the replacement type has the following properties:

  - All const member functions of std::valarray are provided.
  - std::valarray, std::slice_array, std::gslice_array, std::mask_array and std::indirect_array can be constructed from the replacement type.
  - All functions accepting an argument of type const std::valarray& except `begin()` and `end()` (since C++14) should also accept the replacement type.
  - All functions accepting two arguments of type const std::valarray& should accept every combination of const 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.

### [edit] Example

| This section is incomplete Reason: no example

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/numeric/valarray/operator_at&oldid=76056"
Categories:

- Todo with reason
- Todo no example