C++ 對映庫 - crend() 函式



C++ 函式std::map::crend()返回一個指向容器中第一個元素之前理論元素(即對映的反向末尾)的常量反向迭代器。

宣告

以下是來自std::map標頭檔案的std::map::crend()函式的宣告。

C++11

const_reverse_iterator crend() const noexcept;

引數

返回值

返回一個常量反向迭代器。

異常

此成員函式從不丟擲異常。

時間複雜度

常數,即O(1)

示例

以下示例演示了std::map::crend()函式的用法。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   /* Initializer_list constructor */
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   cout << "Map contains following elements in reverse order" << endl;

   for (auto it = m.crbegin(); it != m.crend(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

讓我們編譯並執行以上程式,這將產生以下結果:

Map contains following elements in reverse order
e = 5
d = 4
c = 3
b = 2
a = 1
map.htm
廣告