TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ multiset cend() function

C++ multiset cend() Function with Examples on tutorial for beginners and professionals with examples on multiset, begin(), end(), multiset constructor, ~multiset destructor, operator=, rbegin(), rend() etc.

<< Back to CPP

C++ multiset cend()

C++ multiset cend() function is used to return a constant iterator which is next to the last entry in the multiset.

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

Syntax

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

A const_iterator is an iterator that points to constant content.

Parameter

None

Return value

The cend() function returns a constant iterator which is pointing next to the last element of the multiset.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Concurrently accessing the elements of a multiset container is safe.

Exception Safety

This member function never throws exceptions.

Example 1

Let's see the simple example for cend() function:

#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:

mymultiset contains: 10 10 20 20 30 40

In the above example, cend() function is used to return an iterator pointing next to the last element in the mymultiset multiset.

Example 2

Let's see a simple example to find the element in the multiset:

#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:

Enter value to find: 10
Element found: 10

Example 3

Let's see a simple example to iterate over the multiset using while loop:

#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 it points to
        ++it; // and iterate to the next element
    }
    cout << endl;
}

Output:

Apple
Banana
Orange
Orange

In the above example, cend() function is used to return an iterator pointing next to the last element in the mymultiset multiset.

Example 4

Let's see a simple 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:

1
1
2
3

In the above example, cend() function is used to return an iterator pointing next to the last element in the mymultiset multiset.

Next TopicC++ multiset




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf