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
廣告