Skip to content
Related Articles

Related Articles

Improve Article

unordered_multimap cend() function in C++ STL

  • Last Updated : 08 Aug, 2018

The unordered_multimap::cend() is a built-in function in C++ STL which returns a constant iterator pointing to the position after the last element in the container or to the position after the last element in one of its bucket.

Syntax:

unordered_multimap_name.cend(n)

Parameters: The function accepts one parameter. If a parameter is passed, it returns a constant iterator pointing to the position after the last element in one of its bucket. If no parameter is passed, then it returns a constant iterator pointing to the position after the last element in the unordered_multimap container.

Return Value: It returns a constant iterator. It cannot be used to modify the key and element of the unordered_multimap element.

Below programs illustrates the above function:



Program 1:




// C++ program to illustrate the
// unordered_multimap::cend()
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<int, int> sample;
  
    // inserts key and element
    sample.insert({ 1, 2 });
    sample.insert({ 3, 4 });
    sample.insert({ 3, 4 });
    sample.insert({ 2, 3 });
    sample.insert({ 2, 3 });
  
    // prints all element
    cout << "Key and Elements:";
    for (auto it = sample.cbegin(); it != sample.cend(); it++)
        cout << "\n   " << it->first << "\t      " 
             << it->second;
  
    return 0;
}
Output:
Key and Elements:
   2          3
   2          3
   1          2
   3          4
   3          4

Program 2:




// C++ program to illustrate the
// unordered_multimap::cend(bucket)
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<int, int> sample;
  
    // inserts key and element
    sample.insert({ 1, 2 });
    sample.insert({ 3, 4 });
    sample.insert({ 3, 4 });
    sample.insert({ 2, 3 });
    sample.insert({ 2, 3 });
  
    // prints all element
    cout << "Key and Elements of first bucket:";
    for (auto it = sample.cbegin(1); it != sample.cend(1); it++)
        cout << "\n   " << it->first << "\t      " 
             << it->second;
  
    return 0;
}
Output:
Key and Elements of first bucket:
   1          2
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :