C++ Standard Library C++ STL Library

C++ unordered_multimap - cend() Function



The C++ unordered_multimap::cend function returns the constant iterator (const_iterator) pointing to the past-the-last element of the unordered_multimap container or one of its bucket. The past-the-last element of the multimap 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 multimap element is not itself constant.

Note: There is no guarantee on how elements are ordered in the unordered_multimap (or the bucket). But the range that goes from begin to end contains all elements of the unordered_multimap container (or the bucket).

Syntax

//container iterator
const_iterator cend() const noexcept;

//bucket iterator
const_local_iterator cend ( size_type n ) const;

Parameters

n Specify the bucket number. It should be lower than the bucket_count.

Return Value

A const_iterator to the past-the-last element of the unordered_multimap container or one of its bucket.

Time Complexity

Constant i.e, Θ(1)

Example:

In the example below, the unordered_multimap::cend function returns the const_iterator pointing to the past-the-last element of the unordered_multimap container or one of its bucket. The unordered_multimap::cend function is often used with unordered_multimap::cbegin function to specify a range including all elements of the unordered_multimap container or one of its bucket.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_multimap<string, string> uMMap;
  uMMap = {{"CAN", "Ottawa"}, {"USA", "Washington"}, 
           {"IND", "Delhi"}, {"CAN", "Toronto"}};

  cout<<"uMMap contains: ";
  for(auto cit = uMMap.cbegin(); cit != uMMap.cend(); ++cit)
    cout<<cit->first<<":"<<cit->second<<" ";

  cout<<"\n\nThe uMMap's bucket contains: ";
  for(unsigned int i = 0; i < uMMap.bucket_count(); i++) {
    cout<<"\nBucket #"<<i<<": ";   
    for(auto cit = uMMap.cbegin(i); cit != uMMap.cend(i); ++cit) {
      cout<<cit->first<<":"<<cit->second<<" ";
    } 
  } 
  return 0;
}

The output of the above code will be:

uMMap contains: IND:Delhi USA:Washington CAN:Toronto CAN:Ottawa 

The uMMap's bucket contains: 
Bucket #0: 
Bucket #1: 
Bucket #2: 
Bucket #3: 
Bucket #4: 
Bucket #5: 
Bucket #6: 
Bucket #7: 
Bucket #8: 
Bucket #9: 
Bucket #10: USA:Washington 
Bucket #11: IND:Delhi 
Bucket #12: CAN:Toronto CAN:Ottawa 

❮ C++ <unordered_map> Library