C++ Standard Library C++ STL Library

C++ <valarray> - begin() Function

The C++ <valarray> begin() function returns the iterator pointing to the first element of the valarray.

C++ begin end

The version 1 of the function returns mutable random-access iterator and version 2 of the function returns constant random-access iterator

Please note that, valarray don't have either begin() defined as member function or iterator as member types. This function is an overload of begin(), defined in <iterator>.


//version 1
template <class T> 
  /*unspecified1*/ begin (valarray<T>& x);

//version 2
template <class T> 
  /*unspecified2*/ begin (const valarray<T>& x);


x Specify a valarray object.

Return Value

Returns an random-access iterator to the first element of x.

Time Complexity

Depends on library implementation.


In the example below, the begin() function is used to iterate over a valarray to print it's content.

#include <iostream>
#include <valarray>
using namespace std;
int main (){
  valarray<int> varr{10, 20, 30, 40, 50};

  //print the content of the valarray
  cout<<"varr contains: ";
  for(auto it = begin(varr); it != end(varr); ++it)
    cout<<*it<<" ";

  return 0;

The output of the above code will be:

varr contains: 10 20 30 40 50

❮ C++ <valarray> Library