Home » C++ STL

array::rbegin() and array::rend() functions with Example in C++ STL

C++ STL | array::rbegin() and array::rend() functions: Here, we are going to learn about the array::rbegin() and array::rend() functions of Array in C++ STL.
Submitted by IncludeHelp, on March 01, 2019

C++ STL array::rbegin() and array::rend() functions

array::rbegin() function is a library function of array and it is used to get the first element (from reverse side) of the array, it returns a reverse iterator pointing to the last element of the array.

array::rend() function is a library function of array and it is used to get the last element (from reverse side i.e. first element) of the array, it returns a reverse iterator pointing to the last element of the array.

Syntax:

    array::rbegin();
    array::rend();

Parameters: None

Return value: Function return reverse iterators pointing to the first and last elements of an array.

Example:

    Input or array declaration:
    array<int,5> arr {10, 20, 30, 40, 50};

    Function call:
    auto it=arr.rbegin();
    cout<<*it;
    it=arr.rend();
    cout<<*it;

    Output:
    50 10

C++ STL program to demonstrate example of array::rbegin() and array::rend() functions

#include <array>
#include <iostream>
using namespace std;

int main()
{
	array<int,5> numbers {10, 20, 30, 40, 50};
	array<string,5> cities {"New Delhi", "Mumbai", "Gwalior"};

	cout<<"Elements of numbers array..."<<endl;
	for(auto it=numbers.rbegin(); it!=numbers.rend(); it++)
		cout<<*it<<" ";
	cout<<endl;

	cout<<"Elements of cities array..."<<endl;
	for(auto it=cities.rbegin(); it!=cities.rend(); it++)
		cout<<*it<<" ";
	cout<<endl;    

	return 0;
}

Output

Elements of numbers array...
50 40 30 20 10
Elements of cities array...
Gwalior Mumbai New Delhi





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.