[valarray.sub]

# 29 Numerics library [[numerics]](./#numerics)

## 29.6 Numeric arrays [[numarray]](numarray#valarray.sub)

### 29.6.2 Class template valarray [[template.valarray]](template.valarray#valarray.sub)

#### 29.6.2.5 Subset operations [valarray.sub]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7624)

The member operator[] is overloaded to provide several ways to select
sequences of elements from among those controlled by *this[.](#1.sentence-1)

Each of these
operations returns a subset of the array[.](#1.sentence-2)

The const-qualified versions return this
subset as a new valarray object[.](#1.sentence-3)

The non-const versions return a class
template object which has reference semantics to the original array, working in
conjunction with various overloads of operator= and other assigning
operators to allow selective replacement (slicing) of the controlled sequence[.](#1.sentence-4)

In each case the selected element(s) shall exist[.](#1.sentence-5)

[ð](#lib:operator%5b%5d,valarray_)

`valarray operator[](slice slicearr) const;
`

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7640)

*Returns*: A valarray containing those
elements of the controlled sequence designated by slicearr[.](#2.sentence-1)

[*Example [1](#example-1)*: const valarray<char> v0("abcdefghijklmnop", 16);// v0[slice(2, 5, 3)] returns valarray<char>("cfilo", 5) â *end example*]

[ð](#lib:operator%5b%5d,valarray__)

`slice_array<T> operator[](slice slicearr);
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7658)

*Returns*: An object that holds references to elements of the controlled
sequence selected by slicearr[.](#3.sentence-1)

[*Example [2](#example-2)*: valarray<char> v0("abcdefghijklmnop", 16);
valarray<char> v1("ABCDE", 5);
v0[slice(2, 5, 3)] = v1;// v0 == valarray<char>("abAdeBghCjkDmnEp", 16); â *end example*]

[ð](#lib:operator%5b%5d,valarray___)

`valarray operator[](const gslice& gslicearr) const;
`

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7678)

*Returns*: A valarray containing those
elements of the controlled sequence designated by gslicearr[.](#4.sentence-1)

[*Example [3](#example-3)*: const 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) â *end example*]

[ð](#lib:operator%5b%5d,valarray____)

`gslice_array<T> operator[](const gslice& gslicearr);
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7700)

*Returns*: An object that holds references to elements of the controlled
sequence selected by gslicearr[.](#5.sentence-1)

[*Example [4](#example-4)*: 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) â *end example*]

[ð](#lib:operator%5b%5d,valarray_____)

`valarray operator[](const valarray<bool>& boolarr) const;
`

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7723)

*Returns*: A valarray containing those
elements of the controlled sequence designated by boolarr[.](#6.sentence-1)

[*Example [5](#example-5)*: const valarray<char> v0("abcdefghijklmnop", 16);const bool vb[] = { false, false, true, true, false, true };// v0[valarray<bool>(vb, 6)] returns// valarray<char>("cdf", 3) â *end example*]

[ð](#lib:operator%5b%5d,valarray______)

`mask_array<T> operator[](const valarray<bool>& boolarr);
`

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7743)

*Returns*: An object that holds references to elements of the controlled
sequence selected by boolarr[.](#7.sentence-1)

[*Example [6](#example-6)*: 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) â *end example*]

[ð](#lib:operator%5b%5d,valarray_______)

`valarray operator[](const valarray<size_t>& indarr) const;
`

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7764)

*Returns*: A valarray containing those
elements of the controlled sequence designated by indarr[.](#8.sentence-1)

[*Example [7](#example-7)*: const valarray<char> v0("abcdefghijklmnop", 16);const size_t vi[] = { 7, 5, 2, 3, 8 };// v0[valarray<size_t>(vi, 5)] returns// valarray<char>("hfcdi", 5) â *end example*]

[ð](#lib:operator%5b%5d,valarray________)

`indirect_array<T> operator[](const valarray<size_t>& indarr);
`

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7784)

*Returns*: An object that holds references to elements of the controlled
sequence selected by indarr[.](#9.sentence-1)

[*Example [8](#example-8)*: 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) â *end example*]
