给出的任务是显示C ++ STL中的Deque crend()函数的功能
双端队列是双端队列,它是序列容器,在两端都提供扩展和收缩功能。队列数据结构允许用户仅在END插入数据,并从FRONT删除数据。让我们以在公交车站排队的类比为例,那里的人只能从END插入队列,而站在FRONT的人是第一个被移走的人,而在双头队列中,可以同时插入和删除数据结束。
双端队列crend()函数返回const_reverse_iterator,该指针指向双端队列第一个元素之前的元素,该元素被视为反向端点。
Deque_name.crend( )
deque_crend()函数返回双端队列的const_reverse_iterator。
输入双端队列-5 4 3 2 1
输出双端输出反向-1 2 3 4 5
输入双端 队列-75 45 33 77 12
输出双端输出 反向-12 77 33 45 75
首先我们声明双端队列。
然后我们打印双端队列。
然后我们使用crend()函数。
通过使用上述方法,我们可以以相反的顺序打印双端队列。
// C++ code to demonstrate the working of deque crend( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
//宣布双端队列
Deque<int> deque = { 5, 4, 3, 2, 1 };
//打印双端队列
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
//反向打印双端队列
cout<< “ Deque in reverse order:”;
for( auto x = deque.crend( ) - 1; x >= deque.begin( ); --x)
cout<< “ “ <<*x;
return 0;
}输出结果
如果我们运行上面的代码,那么它将生成以下输出
Input - Deque: 5 4 3 2 1 Output - Deque in reverse order: 1 2 3 4 5
// C++ code to demonstrate the working of crend( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main( ){
deque<char> deque ={ ‘L’ , ‘A’ , ‘P’ , ‘T’ , ‘O’ , ‘P’ };
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
//反向打印双端队列
cout<< “ Deque in reverse order:”;
for( auto x = deque.crend( ) - 1; x >= deque.begin( ); --x)
cout<< “ “ <<*x;
return 0;
}输出结果
如果我们运行上面的代码,那么它将生成以下输出
Input – Deque: L A P T O P Output – Deque in reverse order: P O T P A L