在 C++ STL 中的多重對映 rend


在本文中,我們將討論 C++ STL 中多重對映::rend() 函式的工作原理、語法和示例。

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

多重對映是關聯容器,類似於 map 容器。它還有助於按特定順序儲存由鍵值和對映值組合形成的元素。在多重對映容器中,有多個元素可以關聯到同一鍵。資料在內部總是藉助其關聯鍵進行排序。

什麼是 multimap::rend()?

multimap::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 multimap container.

語法

multiMap_name.rend();

引數

此函式不接受任何引數。

返回值

此函式返回指向 multimap 容器最後一個元素的迭代器。

輸入 

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

輸出 

error

示例

 即時演示

#include <bits/stdc++.h>
using namespace std;
int main(){
   multimap<int, int> mul;
   //inserting elements in multimap
   mul.insert({ 1, 10 });
   mul.insert({ 2, 20 });
   mul.insert({ 3, 30 });
   mul.insert({ 4, 40 });
   mul.insert({ 5, 50 });
   //displaying multimap elements
   cout << "\nElements in multimap is : \n";
   cout << "KEY\tELEMENT\n";
   for (auto it = mul.rbegin(); it!= mul.rend(); ++it){
      cout << it->first << '\t' << it->second << '\n';
   }
   return 0;
}

輸出

如果我們執行以上程式碼,它將生成以下輸出 -

Elements in multimap is :
KEY ELEMENT
5 50
4 40
3 30
2 20
1 10

更新於: 22-4-2020

100 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

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