Set Cend Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Set Cend Function

Set Cend Function

BLUF: Mastering Set Cend Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Set Cend Function

C++ is renowned for its efficiency. Learn how Set Cend Function enables low-level control and high-performance computing in the tutorial below.

C++ set cend

The C++ set cend method is utilized to provide a constant iterator that points to the element following the last entry in the set.

Note:- This is a placeholder. No element exists in this location and attempting to access is undefined behavior.

Syntax

Example

const_iterator cend() const noexcept;  //since C++ 11

A const_iterator is an iterator that points to read-only data in C++ tutorials.

Parameter

Return value

It yields a constant iterator positioned immediately after the final element within the set.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Simultaneously accessing the elements of a set is considered to be secure.

Exception Safety

This member function never throws exceptions.

Example 1

Let's see the simple example for cend function:

Example

#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset = {60,20,40,50,10,30};

  std::cout << "myset contains:";
  for (auto it=myset.cbegin(); it != myset.cend(); ++it)
    std::cout << ' ' << *it;

  std::cout << '\n';

  return 0;
}

Output:

Output

myset contains: 10 20 30 40 50 60

In the given scenario, the cend method is employed to retrieve an iterator indicating the position immediately after the final element in the set named myset.

Example 2

Let's examine a straightforward example to locate the item within the set:

Example

#include <iostream>
#include <string>
#include <set>

using namespace std;

int main ()
{
    int val;
    set<int> c = {10,20,30,40,50};
 
    cout<<"Enter value to find: ";
    cin>>val;

    auto result = c.find(val);  
    
    //find until end of the set elements
    if (result != c.cend()) {  
        cout << "Element found: "<< *result; 
        cout << endl;  
    } else {  
        cout << "Element not found." << endl;  
    }  
    
  return 0;
}

Output:

Output

Enter value to find: 10
Element found: 10

Example 3

Let's examine a straightforward illustration of iterating through a set utilizing a while loop:

Example

#include <iostream>
#include <set>
#include <string>
int main()
{
    using namespace std;
 
    set<string> myset = {"Orange", "Banana", "Apple"};

    set<string>::const_iterator it; // declare an iterator
    
    it = myset.cbegin(); // assign it to the start of the set
    
    while (it != myset.cend()) // while it hasn't reach the end
    {
        cout << *it <<endl; 
        // print the value of the element icpp tutorials to
        ++it; // and iterate to the next element
    }
    cout << endl;
}

Output:

Output

Apple
Banana
Orange

In the previous instance, the cend method is employed to retrieve an iterator indicating the element following the final one in the myset set.

Example 4

Let's see a simple example:

Example

#include <iostream>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
  set<int> c = {3, 1, 2};

  for_each(c.cbegin(), c.cend(), [](const int& x) {
    cout << x << endl;
  });
  
  return 0;
}

Output:

In the given instance, the cend method is employed to retrieve an iterator that points to the element following the final one in the set named myset.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience