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


本文將討論 C++ STL 中 map::rend() 函式的工作原理、語法和示例。

什麼是 C++ STL 中的對映

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

什麼是 map::rend()?

map::rend() 函式是 C++ STL 中的內建函式,它在 header file. rend() implies reverse end function, this function is the reverse of the end(). This function returns an iterator which is pointing to the element preceding the first element of the map container.

語法

Map_name.rend();

引數

此函式不接受任何引數。

返回值

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

示例

輸入

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

輸出

error

示例

 即時演示

#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});
   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;
}

輸出

TP Map is:
MAP_KEY    MAP_ELEMENT
4          70
3          50
2          30
1          10

更新時間: 15-Apr-2020

134 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

立即開始
廣告
© . All rights reserved.