Related Articles

Related Articles

unordered_multimap cbegin() function in C++ STL
  • Last Updated : 08 Aug, 2018

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

Syntax:

unordered_multimap_name.cbegin(n)

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

Return Value: It returns a constant iterator. It cannot be used to change the value of the unordered_multimap element.

Below programs illustrates the above function:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::cbegin() 
#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 key and element
    cout << "Key and Elements : \n";
    for (auto it = sample.begin(); it != sample.end(); it++)
        cout << "   " << it->first << "\t      " 
             << it->second << endl;
  
    auto it = sample.begin();
  
    // print the first element
    cout << "\nThe first element and key: " 
         << it->first << " ";
    cout << it->second;
  
    return 0;
}

chevron_right


Output:

Key and Elements : 
   2          3
   2          3
   1          2
   3          4
   3          4

The first element and key: 2 3

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::cbegin(bucket) 
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<int, int> sample;
  
    // inserts 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: \n";
    for (auto it = sample.cbegin(1); it != sample.cend(1); it++)
        cout << "   " << it->first << "\t      " 
             << it->second << endl;
  
    auto it = sample.cbegin(1);
  
    // print the first element
    cout << "\nThe first element and key in first bucket: "
         << it->first << " ";
    cout << it->second;
  
    return 0;
}

chevron_right


Output:

Key and Elements of first bucket: 
   1          2

The first element and key in first bucket: 1 2

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :