valarray::operator[]
valarray::operator[]
T& operator[](size_t n);
slice_array<T> operator[](slice sa);
gslice_array<T> operator[](const gslice& ga);
mask_array<T> operator[](const valarray<bool>& ba);
indirect_array<T> operator[](const valarray<size_t>& xa);
T operator[](size_t n) const;
valarray<T> operator[](slice sa) const;
valarray<T> operator[](const gslice& ga) const;
valarray<T> operator[](const valarray<bool>& ba) const;
valarray<T> operator[](const valarray<size_t>& xa) const;
The member operator is overloaded to provide several ways to select sequences of elements from among those controlled by *this
. The first group of five member operators works in conjunction with various overloads of operator=
(and other assigning operators) to allow selective replacement (slicing) of the controlled sequence. The selected elements must exist.
The first member operator selects element n
. For example:
valarray<char> v0("abcdefghijklmnop", 16);
v0[3] = 'A';
// v0 == valarray<char>("abcAefghijklmnop", 16)
The second member operator selects those elements of the controlled sequence designated by sa
. For example:
valarray<char> v0("abcdefghijklmnop", 16);
valarray<char> v1("ABCDE", 5);
v0[slice(2, 5, 3)] = v1;
// v0 == valarray<char>("abAdeBghCjkDmnEp", 16)
The third member operator selects those elements of the controlled sequence designated by ga
. For example:
valarray<char> v0("abcdefghijklmnop", 16);
valarray<char> v1("ABCDEF", 6);
const size_t lv[] = {2, 3};
const size_t dv[] = {7, 2};
const valarray<size_t> len(lv, 2), str(dv, 2);
v0[gslice(3, len, str)] = v1;
// v0 == valarray<char>("abcAeBgCijDlEnFp", 16)
The fourth member operator selects those elements of the controlled sequence designated by ma
. For example:
valarray<char> v0("abcdefghijklmnop", 16);
valarray<char> v1("ABC", 3);
const bool vb[] = {false, false, true, true, false, true};
v0[valarray<bool>(vb, 6)] = v1;
// v0 == valarray<char>("abABeCghijklmnop", 16)
The fifth member operator selects those elements of the controlled sequence designated by ia
. For example:
valarray<char> v0("abcdefghijklmnop", 16);
valarray<char> v1("ABCDE", 5);
const size_t vi[] = {7, 5, 2, 3, 8};
v0[valarray<size_t>(vi, 5)] = v1;
// v0 == valarray<char>("abCDeBgAEjklmnop", 16)
In the second group, each of the five member operators constructs an object that represents the value(s) selected. The selected elements must exist.
The sixth member operator returns the value of element n
. For example:
valarray<char> v0("abcdefghijklmnop", 16);
// v0[3] returns 'd'
The seventh member operator returns an object of class valarray<T>
containing those elements of the controlled sequence designated by sa
. For example:
valarray<char> v0("abcdefghijklmnop", 16);
// v0[slice(2, 5, 3)] returns valarray<char>("cfilo", 5)
The eighth member operator selects those elements of the controlled sequence designated by ga
. For example:
valarray<char> v0("abcdefghijklmnop", 16);
const size_t lv[] = {2, 3};
const size_t dv[] = {7, 2};
const valarray<size_t> len(lv, 2), str(dv, 2);
// v0[gslice(3, len, str)] returns valarray<char>("dfhkmo", 6)
The ninth member operator selects those elements of the controlled sequence designated by ma
. For example:
valarray<char> v0("abcdefghijklmnop", 16);
const bool vb[] = {false, false, true, true, false, true};
// v0[valarray<bool>(vb, 6)] returns valarray<char>("cdf", 3)
The last member operator selects those elements of the controlled sequence designated by ia
. For example:
valarray<char> v0("abcdefghijklmnop", 16);
const size_t vi[] = {7, 5, 2, 3, 8};
// v0[valarray<size_t>(vi, 5)] returns valarray<char>("hfcdi", 3)