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

Multiset Cend Function

BLUF: Mastering Multiset 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: Multiset Cend Function

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

C++ multiset cend

The cend function in C++ for multiset returns a constant iterator pointing to the element following the last element in the multiset.

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

The cend function provides a read-only iterator that points to the element after the last one in the multiset.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Simultaneously retrieving the elements from a multiset container is considered to be a secure operation.

Exception Safety

This member function never throws exceptions.

Example 1

Let's see the simple example for cend function:

Example

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  multiset<int> mymultiset = {20,20,30,40,10,10};

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

  cout << '\n';

  return 0;
}

Output:

Output

mymultiset contains: 10 10 20 20 30 40

In the previous case, the cend method is employed to retrieve an iterator pointing to the element immediately following the last element in the mymultiset multiset.

Example 2

Let's explore a straightforward example to locate an element within a multiset:

Example

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

using namespace std;

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

    auto result = c.find(val);  
    
    //find until end of the multiset 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 explore a basic illustration of iterating through the multiset using a while loop:

Example

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

    multiset<string>::const_iterator it; // declare an iterator
    
    it = mymultiset.cbegin(); // assign it to the start of the multiset
    
    while (it != mymultiset.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
Orange

In the preceding example, the cend method is employed to retrieve an iterator that points to the element following the last one in the mymultiset multiset.

Example 4

Let's see a simple example:

Example

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

using namespace std;

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

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

Output:

Output

1
1
2
3

In the provided instance, the cend method is employed to retrieve an iterator indicating the position immediately following the final element within the mymultiset multiset.

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