multiset crbegin() and crend() function in C++ STL

  1. The multiset::crbegin() is a built-in function in C++ STL which returns a constant reverse iterator pointing to the last element in the container. The iterator cannot be used to modify the elements in the multiset container. The iterators can be increased or decreased to traverse the set accordingly.

    Syntax:

    constant_reverse_iterator multiset_name.cregin()
    

    Parameters: The function does not take any parameter.

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

    Below programs illustrate the multiset::crbegin() method.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate the
    // multiset::crbegin() function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
      
        int arr[] = { 14, 10, 15, 11, 10 };
      
        // initializes the set from an array
        multiset<int> s(arr, arr + 5);
      
        cout << "The last element: " << *(s.crbegin()) << endl;
        // prints all elements in set
        for (auto it = s.crbegin(); it != s.crend(); it++)
            cout << *it << " ";
      
        return 0;
    }

    chevron_right

    
    

    Output:

    The last element: 15
    15 14 11 10 10
    
  2. The multiset::crend() is a built-in function in C++ STL which returns a constant reverse iterator pointing to the position just before the first element in the container. The iterator cannot be used to modify the elements in the multiset container. The iterators can be increased or decreased to traverse the set accordingly.

    Syntax:

    constant_reverse_iterator multiset_name.crend()
    

    Parameters: The function does not take any parameter.

    Return value: The function returns a constant iterator pointing to the position just before the first element in the multiset container.

    Below programs illustrate the multiset::crend() method.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to demonstrate the
    // multiset::crend() function
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
      
        int arr[] = { 14, 12, 15, 11, 10, 10, 16, 16 };
      
        // initializes the set from an array
        multiset<int> s(arr, arr + 8);
      
        // prints all elements in set
        for (auto it = s.crbegin(); it != s.crend(); it++)
            cout << *it << " ";
      
        return 0;
    }

    chevron_right

    
    

    Output:

    16 16 15 14 12 11 10 10
    


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.