rbegin rend_以相反的顺序迭代列表(list :: rbegin()和list :: rend()函数的示例) C ++ STL...

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


翻译自: https://www.includehelp.com/stl/iterate-a-list-in-reverse-order-example-of-list-rbegin-and-list-rend-functions.aspx

rbegin rend

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值