當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ map rbegin()用法及代碼示例


std::map::rbegin()是C++ STL中的一個函數。它返回一個反向迭代器,該迭代器指向Map的最後一個元素。反向迭代器以相反的順序進行迭代,遞增迭代器意味著朝著Map的開頭移動。

用法:

r_i rbegin();
const_r_i rbegin() const;

參數:它不排除任何參數。


返回值:它返回一個反向迭代器,該迭代器指向Map的最後一個元素。

時間複雜度:O(1)

以下示例說明了map::rbegin()方法:

示例1:

// C++ Program to illustrate 
// map::rbegin() method 
  
#include <iostream> 
#include <map> 
using namespace std; 
  
int main() 
{ 
  
    map<char, int> mp = { 
        { 'a', 1 }, 
        { 'b', 2 }, 
        { 'c', 3 }, 
        { 'd', 4 }, 
        { 'e', 5 }, 
    }; 
  
    cout << "Map contains following "
         << "elements in reverse order"
         << endl; 
  
    for (auto i = mp.rbegin(); i != mp.rend(); ++i) 
        cout << i->first 
             << " = " << i->second 
             << endl; 
  
    return 0; 
}
輸出:
Map contains following elements in reverse order
e = 5
d = 4
c = 3
b = 2
a = 1

示例2:

// C++ Program to illustrate 
// map::rbegin() method 
  
#include <iostream> 
#include <map> 
using namespace std; 
  
int main() 
{ 
  
    map<char, char> mp = { 
        { 'a', 'A' }, 
        { 'b', 'B' }, 
        { 'c', 'C' }, 
        { 'd', 'D' }, 
        { 'e', 'E' }, 
    }; 
  
    cout << "Map contains following "
         << "elements in reverse order"
         << endl; 
  
    for (auto i = mp.rbegin(); i != mp.rend(); ++i) 
        cout << i->first 
             << " = " << i->second 
             << endl; 
  
    return 0; 
}
輸出:
Map contains following elements in reverse order
e = E
d = D
c = C
b = B
a = A


相關用法


注:本文由純淨天空篩選整理自lakshita大神的英文原創作品 map rbegin() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。