在本文中,我们将讨论C ++ STL中list::rbegin()和list::rend()函数的工作,语法和示例。
列表是一种数据结构,允许按时间顺序在任意位置进行插入和删除。列表被实现为双向链接列表。列表允许非连续的内存分配。与数组,向量和双端队列相比,列表在容器中的任何位置执行元素的插入提取和移动效果都更好。在列表中,对元素的直接访问很慢,列表类似于forward_list,但是转发列表对象是单个链接列表,并且只能迭代转发。
list::rbegin()是C ++ STL中的内置函数,在头文件中声明。rbegin()是反向开始功能。rebegin()返回一个反向迭代器,该迭代器指向列表的最后一个元素。反向迭代器是一种迭代器,它从末尾开始以相反的方向移动,并将朝着起点移动。但是back()也返回最后一个元素,但与简单迭代器不同,此双向迭代器向后移动。
list_container1.rbegin();
此函数不接受任何参数。
Input: list<int> List_container = {10, 11, 13, 15};
List_container.rbegin();
Output:
List= 15该函数返回指向列表的最后一个元素的反向迭代器。反向迭代器是向后移动的迭代器。
#include <bits/stdc++.h>
using namespace std;
int main(){
list<int> myList = { 10, 20, 30, 40 };
cout<<"List is: ";
for (auto i = myList.rbegin(); i!= myList.rend(); ++i)
cout << *i << " ";
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
List is: 40 30 20 10
list::rend()是C ++ STL中的内置函数,在头文件中声明。rend()是一个反向功能。rend()返回一个反向迭代器,该迭代器指向关联的列表容器的第一个元素之前的位置。反向迭代器是一种迭代器,它从末尾开始以相反的方向移动,并将朝着起点移动。但是back()也返回最后一个元素,但与简单迭代器不同,此双向迭代器向后移动。
list_container1.rend();
此函数不接受任何参数。
Input: list<int> List_container= { 10, 11, 13, 15};
List_container.rend();
Output:
List= 5 //will display random value which is before the beginning of the list此函数返回一个反向迭代器,该迭代器指向列表中第一个元素之前的元素。反向迭代器是向后移动的迭代器。
#include <bits/stdc++.h>
using namespace std;
int main(){
list<int> myList = { 10, 20, 30, 40 };
cout<<"List is : ";
for (auto i = myList.rbegin(); i!= myList.rend(); ++i)
cout << *i << " ";
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
List is: 40 30 20 10