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

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

    Syntax:

    vector_name.rbegin()

    Parameters: The function does not accept any parameter.

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

    Program to demonstrate the vector::rbegin() method:

    Program 1:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to illustrate
    // the vector::rbegin() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        vector<int> v;
        v.push_back(11);
        v.push_back(12);
        v.push_back(13);
        v.push_back(14);
        v.push_back(15);
      
        // prints all the elements
        cout << "The vector elements in reverse order are:\n";
        for (auto it = v.rbegin(); it != v.rend(); it++)
            cout << *it << " ";
        return 0;
    }

    chevron_right

    
    

    Output:

    The vector elements in reverse order are:
    15 14 13 12 11
    
  2. vector::rend() is a built-in function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first element in the array container.

    Syntax:

    vector_name.rend()

    Parameters: The function does not take any parameter.

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

    Program to demonstrate the vector::rend() method:

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to illustrate
    // the vector::rend() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        vector<int> v;
        v.push_back(11);
        v.push_back(12);
        v.push_back(13);
        v.push_back(14);
        v.push_back(15);
      
        cout << "The last element is: " << *v.rbegin();
      
        // prints all the elements
        cout << "\nThe vector elements in reverse order are:\n";
        for (auto it = v.rbegin(); it != v.rend(); it++)
            cout << *it << " ";
        return 0;
    }

    chevron_right

    
    

    Output:

    The last element is: 15
    The vector elements in reverse order are:
    15 14 13 12 11
    


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 :


1


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