public member function
<valarray>

std::valarray::operator[]

element (1)
               T  operator[] (size_t n) const;               T& operator[] (size_t n);
sub-array (2)
      valarray<T> operator[] (slice slc) const;   slice_array<T> operator[] (slice slc);      valarray<T> operator[] (const gslice& gslc) const;  gslice_array<T> operator[] (const gslice& gslc);      valarray<T> operator[] (const valarray<bool>& msk) const;    mask_array<T> operator[] (const valarray<bool>& msk);      valarray<T> operator[] (const valarray<size_t>& ind) const;indirect_array<T> operator[] (const valarray<size_t>& ind);
element (1)
         const T& operator[] (size_t n) const;               T& operator[] (size_t n);
subscript (2)
      valarray<T> operator[] (slice slc) const;   slice_array<T> operator[] (slice slc);      valarray<T> operator[] (const gslice& gslc) const;  gslice_array<T> operator[] (const gslice& gslc);      valarray<T> operator[] (const valarray<bool>& msk) const;    mask_array<T> operator[] (const valarray<bool>& msk);      valarray<T> operator[] (const valarray<size_t>& ind) const;indirect_array<T> operator[] (const valarray<size_t>& ind);
Access element or subscript
The element access versions (1) return the n-th element in the valarray object.

The subscript access versions (2) return a sub-array object that selects the elements specified by its argument:
- If the valarray is const-qualified, the function returns a new valarray object with a copy of this selection.
- Otherwise, the function returns a sub-array object, which has reference semantics to the original array, ready to be used as an l-value.

Parameters

n
Position of an element in the valarray.
Notice that the first element has the position 0, not 1.
Values equal or greater than the object's size cause undefined behavior.
size_t is an unsigned integral type.
slc
A slice object specifying which elements of the valarray are selected.
gslc
A gslice object specifying which elements of the valarray are selected.
msk
A valarray<bool> with its elements identifying whether each element of *this is selected or not: If an element in *this has its corresponding element in msk set to true it is part of the returned sub-array, otherwise it is not.
ind
A valarray<size_t> with its elements identifying which elements of *this are selected: Each element in ind is the index of an element in *this that will be part of the returned sub-array.
size_t is an unsigned integral type.

Return value

An element or a sub-array of *this.
See slice_array, gslice_array, mask_array, indirect_array for details on these returned types.

The versions returning sub-arrays (2) are allowed to return an object of a different type instead of a valarray. Such a type is required to be implicitly convertible to valarray and be supported as argument for all functions taking valarray& arguments. This allows copy-on-write implementations.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// valarray::operator[] example
#include <iostream>     // std::cout
#include <valarray>     // std::valarray, std::slice, std::gslice

int main ()
{
  std::valarray<int> myarray (10);             //  0  0  0  0  0  0  0  0  0  0

  // slice:
  myarray[std::slice(2,3,3)]=10;               //  0  0 10  0  0 10  0  0 10  0

  // gslice:
  size_t lengths[]={2,2};
  size_t strides[]={6,2};
  myarray[std::gslice(1, std::valarray<size_t>(lengths,2), std::valarray<size_t>(strides,2))]=20;
                                               //  0 20 10 20  0 10  0 20 10 20

  // mask:
  std::valarray<bool> mymask (10);
  for (int i=0; i<10; ++i) mymask[i]= ((i%2)==0);
  myarray[mymask] += std::valarray<int>(3,5);  //  3 20 13 20  3 10  3 20 13 20

  //indirect:
  size_t sel[]= {2,5,7};
  std::valarray<size_t> myselection (sel,3);   //  3 20 99 20  3 99  3 99 13 20
  myarray[myselection]=99;

  std::cout << "myarray: ";
  for (size_t i=0; i<myarray.size(); ++i)
	  std::cout << myarray[i] << ' ';
  std::cout << '\n';

  return 0;
}

Output:

3 20 99 20 3 99 3 99 13 20


Complexity

Depends on library implementation.

Iterator validity

No changes: Other valid iterators, references and sub-arrays keep their validity.

Data races

The valarray is accessed, and in some library implementations, the non-const versions may also modify it and all its elements (such as in copy-on-reference implementations).
When a reference or an object with reference semantics is returned, it can be used to access or modify the elements of the valarray.

Exception safety

If the function implementation performs operations on the elements, and any such operation throws an exception, it causes undefined behavior.
If the function needs to allocate storage and fails, it may throw an exception (such as bad_alloc), although this is not mandated.

See also