C++ Array::crend() 函数



C++ 的std::array::crend()函数用于返回一个指向数组第一个元素之前元素的常量反向迭代器。此函数用于反向遍历数组而不修改其元素。

crend() 函数通常与 crbegin() 函数结合使用以创建反向迭代范围。

语法

以下是 std::array::crend() 函数的语法。

const_reverse_iterator crend() const noexcept;

参数

返回值

它返回一个指向数组末尾之后元素的反向常量迭代器。

异常

此函数从不抛出异常。

时间复杂度

常数,即 O(1)

示例 1

在下面的示例中,我们将考虑 crend() 函数的基本用法。

#include <iostream>
#include <array>
using namespace std;
int main(void) {
   array < int, 5 > arr = {10,20,30,40,50};
   auto s = arr.crbegin();
   auto e = arr.crend();
   while (s < e) {
      cout << * s << " ";
      ++s;
   }
   cout << endl;
   return 0;
}

输出

以上代码的输出如下:

50 40 30 20 10

示例 2

考虑下面的示例,我们将对字符串数组应用 crend() 函数。

#include <iostream>
#include <array>
using namespace std;
int main() {
   array < string, 3 > MyArray {"Tutorials","point","company"};
   array < string, 3 > ::const_reverse_iterator crit;
   crit = MyArray.crend();
   crit--;
   cout << * crit << " ";
   crit--;
   cout << * crit << " ";
   crit--;
   cout << * crit << " ";
   return 0;
}

输出

以下是上述代码的输出:

Tutorials point company
array.htm
广告
© . All rights reserved.