C++ STL中的array::crbegin()和array::crend()

2021年4月22日15:16:39 发表评论 263 次浏览

``array_name.crbegin()``

``````//CPP program to illustrate
//the array::crbegin() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
//array initialisation
array<int , 5> arr = { 1, 5, 2, 4, 7 };

//prints the last element
cout <<"The last element is " <<*(arr.crbegin()) <<endl;

//prints all the elements
cout <<"The array elements in reverse order are:\n" ;
for ( auto it = arr.crbegin(); it != arr.crend(); it++)
cout <<*it <<" " ;

return 0;
}``````

``````The last element is 7
The array elements in reverse order are:
7 4 2 5 1``````

``array_name.crend()``

``````//CPP program to illustrate
//the array::crend() function
#include <bits/stdc++.h>
using namespace std;

int main()
{
array<int , 5> arr = { 1, 5, 2, 4, 7 };

//prints all the elements
cout <<"The array elements in reverse order are:\n" ;
for ( auto it = arr.crbegin(); it != arr.crend(); it++)
cout <<*it <<" " ;
return 0;
}``````

``````The array elements in reverse order are:
7 4 2 5 1``````