- 数组::rbegin() 是C++ STL中的一个内置函数,它返回指向容器中最后一个元素的反向迭代器。
语法:
array_name.rbegin()
参数: 该函数不接受任何参数。
返回值: 该函数返回指向容器中最后一个元素的反向迭代器。
用于演示array::rbegin()方法的程序:
项目1:
// CPP program to illustrate
// the array::rbegin() 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.rbegin()) << endl;
// prints all the elements
cout <<
"The array elements in reverse order are:"
;
for
(
auto
it = arr.rbegin(); it != arr.rend(); it++)
cout << *it <<
" "
;
return
0;
}
输出:The last element is 7 The array elements in reverse order are: 7 4 2 5 1
- 数组::rend() 是C++ STL中的一个内置函数,它返回一个反向迭代器,指向数组容器中第一个元素之前的理论元素。
语法:
array_name.rend()
参数: 该函数不接受任何参数。
返回值: 该函数返回一个反向迭代器,指向数组容器中第一个元素之前的理论元素。
用于演示array::rend()方法的程序:
项目1:
// CPP program to illustrate
// the array::rend() 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:"
;
for
(
auto
it = arr.rbegin(); it != arr.rend(); it++)
cout << *it <<
" "
;
return
0;
}
输出:The array elements in reverse order are: 7 4 2 5 1
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END