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



描述

C++ 函式std::map::rend()返回一個反向迭代器,它指向對映的反向末尾,也就是對映的開頭。

宣告

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

C++98

reverse_iterator rend();
const_reverse_iterator rend() const;

C++11

reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;

引數

返回值

如果物件是常量限定的,則方法返回常量反向迭代器;否則返回非常量反向迭代器。

異常

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

時間複雜度

常數,即 O(1)

示例

以下示例顯示了std::map::rend()函式的使用方法。

#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.rbegin(); it != m.rend(); ++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
廣告
© . All rights reserved.