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

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

array::crbegin()

是C++ STL中的内置函数, 它返回一个常量反向迭代器, 指向容器中的最后一个元素。

语法如下:

array_name.crbegin()

参数:该函数不接受任何参数。

返回值:该函数返回指向容器中最后一个元素的反向迭代器。

程序演示array::crbegin()方法:

程序1:

//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::crend()

是C++ STL中的内置函数, 它返回一个常数反向迭代器, 该迭代器指向数组容器中第一个元素之前的理论元素。

语法如下:

array_name.crend()

参数:该函数不接受任何参数。

返回值:该函数返回一个常量反向迭代器, 该迭代器指向数组容器中第一个元素之前的理论元素。

程序演示array::crend()方法:

程序1:

//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

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: