multiset rbegin() and rend() function in C++ STL

  1. multiset::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the multiset container.

    Syntax:

    reverse_iterator multiset_name.rbegin()
    

    Parameters: The function does not take any parameter.

    Return value: The function returns a reverse iterator pointing to the last element in the container.

    Below program illustrate the multiset::rbegin() method:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate the
    // multiset::rbegin() function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
      
        int arr[] = { 15, 12, 15, 11, 10, 10 };
      
        // initializes the set from an array
        multiset<int> s(arr, arr + 6);
      
        multiset<int>::reverse_iterator rit;
      
        // prints all elements in reverse order
        for (rit = s.rbegin(); rit != s.rend(); rit++)
            cout << *rit << " ";
      
        cout << "\nThe last element in multiset is " << *(s.rbegin());
      
        return 0;
    }
    chevron_right
    
    
    Output:

    15 15 12 11 10 10 
    The last element in multiset is 15
    
  2. multiset::rend() in an inbuilt function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first element in the multiset container.

    Syntax:

    reverse_iterator multiset_name.rend()
    

    Parameter: The function does not accepts any parameter.

    Return value: The function returns a reverse iterator pointing to the theoretical element right before the first element in the multiset container.

    Below program illustrate the multiset::rend() function:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate the
    // multiset::rend() function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
      
        int arr[] = { 15, 13, 15, 11, 13, 10 };
      
        // initializes the set from an array
        multiset<int> s(arr, arr + 6);
      
        multiset<int>::reverse_iterator rit;
      
        // prints all elements in reverse order
        for (rit = s.rbegin(); rit != s.rend(); rit++)
            cout << *rit << " ";
      
        return 0;
    }
    chevron_right
    
    
    Output:
    15 15 13 13 11 10
    

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.




Recommended Posts:

    Article Tags :
    C++
    Practice Tags :