function template
<valarray>

std::begin (valarray)

(1)
template <class T> /*unspecified1*/ begin (valarray<T>& x);
(2)
template <class T> /*unspecified2*/ begin (const valarray<T>& x);
Iterator to beginning
Returns an iterator pointing to the first element in the valarray x.

Note that valarray objects have no member function begin defined nor have iterator member types: The iterator returned is a random-access iterator of an unspecified type whose value_type is T and its reference type is T& for (1) and const T& for (2) (it is a mutable iterator for (1), and a constant iterator for (2)).

If the object is an empty valarray, the returned value shall not be dereferenced.

This is an overload of begin, defined in <iterator> (among other headers).

Parameters

x
A valarray object.

Return Value

A random-access iterator to the first element of x.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// begin/end valarray
#include <iostream>     // std::cout
#include <valarray>     // std::valarray

int main ()
{
  std::valarray<int> foo {10,20,30,40,50};

  std::cout << "foo contains:";
  for (auto it = begin(foo); it!=end(foo); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

Output:
foo contains: 10 20 30 40 50


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, version (1) may also modify it (such as in copy-on-reference implementations).
The iterator returned 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