Open In App

multimap::cbegin() and multimap::cend() in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report
  1. multimap::cbegin() is a built-in function in C++ STL which returns a constant iterator referring to the first element in the multimap container. Since multimap container contains the element in an ordered way, cbegin() will point to that element that will come first according to the container’s sorting criterion. Syntax: 
multimap_name.cbegin()
  1. Parameters: The function does not accept any parameter. Return Value: The function returns a constant iterator referring to the first element in the multimap container. 

CPP




// C++ program to illustrate
// the multimap::cbegin() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // initialize container
    multimap<int, int> mp;
 
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 3, 60 });
    mp.insert({ 4, 20 });
    mp.insert({ 5, 50 });
 
    auto ite = mp.cbegin();
 
    cout << "The first element is: ";
    cout << "{" << ite->first << ", "
         << ite->second << "}\n";
 
    // prints the elements
    cout << "\nThe multimap is : \n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.cbegin(); itr != mp.cend(); ++itr) {
        cout << itr->first
             << '\t' << itr->second << '\n';
    }
    return 0;
}


Output:

The first element is: {1, 40}

The multimap is : 
KEY    ELEMENT
1    40
2    30
3    60
4    20
5    50
  1. multimap::cend() is a builtin function in C++ STL which returns a constant iterator pointing to the theoretical element that follows last element in the multimap. Since multimap container contains the element in an ordered way, cend() will point to that follows the last element according to the container’s sorting criterion. Syntax: 
multimap_name.cend()
  1. Parameters: The function does not accept any parameter. Return Value: The function returns a constant iterator pointing to the theoretical element that follows the last element in the multimap. 

CPP




// C++ program to illustrate
// the multimap::cend() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // initialize container
    multimap<int, int> mp;
 
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 3, 60 });
    mp.insert({ 4, 20 });
    mp.insert({ 5, 50 });
 
    // print the elements
    cout << "\nThe multimap is : \n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.cbegin(); itr != mp.cend(); ++itr) {
        cout << itr->first
             << '\t' << itr->second << '\n';
    }
    return 0;
}


Output:

The multimap is : 
KEY    ELEMENT
1    40
2    30
3    60
4    20
5    50

Let us see the differences in a tabular form -:

  multimap::cbegin() multimap::cend()
1. It is used to return a const_iterator pointing to the first element in the container. It is used to return a const_iterator pointing to the past-the-end element in the container.
2.

Its syntax is -:

const_iterator cbegin();

Its syntax is -:

const_iterator cend();

3. It does not take any parameters. It does not take any parameters.
4. Its complexity is constant. Its complexity is constant.
5. Its iterator validity does not changes. Its iterator validity does not changes.


Last Updated : 12 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads