[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 v0("abcdefghijklmnop", 16);// v0[slice(2, 5, 3)] returns valarray("cfilo", 5) — *end example*] [🔗](#lib:operator%5b%5d,valarray__) `slice_array 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 v0("abcdefghijklmnop", 16); valarray v1("ABCDE", 5); v0[slice(2, 5, 3)] = v1;// v0 == valarray("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 v0("abcdefghijklmnop", 16);const size_t lv[] = { 2, 3 };const size_t dv[] = { 7, 2 };const valarray len(lv, 2), str(dv, 2);// v0[gslice(3, len, str)] returns// valarray("dfhkmo", 6) — *end example*] [🔗](#lib:operator%5b%5d,valarray____) `gslice_array 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 v0("abcdefghijklmnop", 16); valarray v1("ABCDEF", 6);const size_t lv[] = { 2, 3 };const size_t dv[] = { 7, 2 };const valarray len(lv, 2), str(dv, 2); v0[gslice(3, len, str)] = v1;// v0 == valarray("abcAeBgCijDlEnFp", 16) — *end example*] [🔗](#lib:operator%5b%5d,valarray_____) `valarray operator[](const valarray& 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 v0("abcdefghijklmnop", 16);const bool vb[] = { false, false, true, true, false, true };// v0[valarray(vb, 6)] returns// valarray("cdf", 3) — *end example*] [🔗](#lib:operator%5b%5d,valarray______) `mask_array operator[](const valarray& 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 v0("abcdefghijklmnop", 16); valarray v1("ABC", 3);const bool vb[] = { false, false, true, true, false, true }; v0[valarray(vb, 6)] = v1;// v0 == valarray("abABeCghijklmnop", 16) — *end example*] [🔗](#lib:operator%5b%5d,valarray_______) `valarray operator[](const valarray& 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 v0("abcdefghijklmnop", 16);const size_t vi[] = { 7, 5, 2, 3, 8 };// v0[valarray(vi, 5)] returns// valarray("hfcdi", 5) — *end example*] [🔗](#lib:operator%5b%5d,valarray________) `indirect_array operator[](const valarray& 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 v0("abcdefghijklmnop", 16); valarray v1("ABCDE", 5);const size_t vi[] = { 7, 5, 2, 3, 8 }; v0[valarray(vi, 5)] = v1;// v0 == valarray("abCDeBgAEjklmnop", 16) — *end example*]