C++ Standard Library C++ STL Library

C++ <valarray> - end() Function

The C++ <valarray> end() function returns the iterator pointing to the past-the-last element of the valarray. The past-the-last element of a sequence is the theoretical element that follows the last element. It does not point to any element, and hence could not be dereferenced.

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 end() defined as member function or iterator as member types. This function is an overload of end(), defined in <iterator>.


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

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


x Specify a valarray object.

Return Value

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

Time Complexity

Depends on library implementation.


In the example below, the end() 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