CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 23 Jul 2025 01:26:36 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/valarray/valarray/operator[]/
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 23 Jul 2025 01:26:37 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"8fca-OghDoDP68x3jMmPMVV3wdhdouoY"
Content-Encoding: gzip
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.
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
Output:
When a reference or an object with reference semantics is returned, it can be used to access or modify the elements of the valarray.
If the function needs to allocate storage and fails, it may throw an exception (such as bad_alloc), although this is not mandated.
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
valarray
- valarray::~valarray
- valarray operators
- valarray::valarray
member functions
non-member overloads
-
begin (valarray)C++11
-
end (valarray)C++11
-
swap (valarray)C++11
-
- Reference
- <valarray>
- valarray
- operator[]
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 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 position0
, not1
.
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 totrue
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
|
|
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
- slice
- Valarray slice selector (class)
- gslice
- Valarray generalized slice selector (class)
- valarray::shift
- Shift elements (public member function)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us