C++ STL 中的 map rbegin() 函式


在這篇文章中,我們將討論 C++ STL 中 map::rbegin() 函式的工作原理、語法和示例。

什麼是 C++ STL 中的對映?

對映是關聯容器,它有助於按特定順序儲存由鍵值和對映值組合形成的元素。在對映容器中,資料始終在與關聯鍵的幫助下在內部進行排序。對映容器中的值是透過其唯一鍵訪問的。

什麼是 map::rbegin() ?

map::rbegin() 函式是 C++ STL 中的一個內建函式,在中定義 header file. rbegin() implies reverse begin function, this function is the reverse of the begin(). This function returns an iterator which is pointing to the last element of the map container.

語法

Map_name.rbegin();

引數

該函式不接受任何引數。

返回值

該函式返回指向對映容器最後一個元素的迭代器。

示例

輸入

map<char, int> newmap;
newmap[‘a’] = 1;
newmap[‘b’] = 2;
newmap[‘c’] = 3;
newmap.rbegin();

輸出

c:3

示例

 線上演示

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP_Map;
   TP_Map.insert({3, 50});
   TP_Map.insert({2, 30});
   TP_Map.insert({1, 10});
   TP_Map.insert({4, 70});
   //using map::rbegin to fetch first last element
   auto temp = TP_Map.rbegin();
   cout<<"First element is: "<<temp->first << " -> " << temp->second;
   cout<<"\nTP Map is : \n";
   cout << "MAP_KEY\tMAP_ELEMENT\n";
   for (auto i = TP_Map.rbegin(); i!= TP_Map.rend(); i++) {
      cout << i->first << "\t" << i->second << endl;
   }
   return 0;
}

輸出

First element is: 4 -> 70
TP Map is:
MAP_KEY    MAP_ELEMENT
4             70
3             50
2             30
1             10

更新時間:15-4-2020

113 次瀏覽

開啟你的 職業生涯

透過完成本課程獲得認證

開始學習
廣告
© . All rights reserved.