Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ forward_list::cend function is used to return the constant iterator (const_iterator) pointing to the past-the-last element of the forward_list container. The past-the-last element of the forward_list is the theoretical element that follows the last element. It does not point to any element, and hence could not be dereferenced.

C++ cbegin cend

Note: A const_iterator is an iterator that points to constant value. The difference between iterator and const_iterator is that the const_iterator cannot be used to modify the contents it points to, even if the forward_list element is not itself constant.

Syntax

const_iterator cend() const noexcept;

Parameters

No parameter is required.

Return Value

A const_iterator to the past-the-last element of the sequence container.

Time Complexity

Constant i.e, Θ(1).

Example:

In the below example, the forward_list::cend function is used to return the const_iterator pointing to the past-the-last element of the forward_list f_list. The forward_list::cend function is often used with forward_list::cbegin function to specify a range including all elements of the forward_list container.

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  forward_list<int> f_list{10, 20, 30, 40, 50};
  forward_list<int>::const_iterator cit;

  for(cit = f_list.cbegin(); cit != f_list.cend(); ++cit)
    cout<<*cit<<" ";

  return 0;
}

The output of the above code will be:

10 20 30 40 50 

❮ C++ - Forward List