rbegin rend
Given a list and we have to iterate it’s all elements in reverse order and print in new line in C ++ STL.
给定一个列表,我们必须以相反的顺序迭代所有元素,并在C ++ STL中以新行打印。
列出reverse_iterator (List reverse_iterator)
To iterate a list in reverse order in C++ STL, we need an reverse_iterator that should be initialized with the last element of the list, as a reverse order and we need to check it till the end of the list.
要在C ++ STL中以相反的顺序迭代列表 ,我们需要一个reverse_iterator ,它应该以列表的最后一个元素初始化,作为相反的顺序,我们需要检查它直到列表的末尾。
List reverse iterator declaration:
列出反向迭代器声明:
list<int>::reverse_iterator revit;
list :: rbegin()和list :: rend()函数 (list::rbegin() and list::rend() functions)
The function list::rbegin() returns a reverse iterator pointing to the first element from the end or we can say as the reverse beginning.
函数list :: rbegin()返回一个反向迭代器,该迭代器从末尾指向第一个元素,或者可以说是反向开始。
Syntax:
句法:
list_name.rbegin();
The function list::rend() returns a reverse iterator pointing to the last element from the end or we can say as the reverse ending.
函数list :: rend()返回一个反向迭代器,该迭代器指向末尾的最后一个元素,或者我们可以说是反向结束。
Syntax:
句法:
list_name.rend();
Example:
例:
Input:
list num{10, 20, 30, 40, 50}
Output:
List elements are:
50
40
30
20
10
Program:
程序:
#include <iostream>
#include <list>
using namespace std;
int main() {
// declare a list
list<int>num {10, 20, 30, 40, 50};
//declare an iterator
list<int>:: reverse_iterator revit;
//run loop using begin () end () functons
cout<< "List elements are: " <<endl;
for( revit=num.rbegin(); revit!=num.rend(); revit++ )
cout<< *revit<<endl;
return 0;
}
Output
输出量
List elements are:
50
40
30
20
10
rbegin rend