C++ multimap rend() 函數用於以相反的順序返回一個迭代器到multimap的末尾(不是最後一個元素,而是過去的最後一個元素)。這類似於非逆向容器的第一個元素之前的元素。
注意:-這是一個占位符。此位置不存在元素,嘗試訪問是未定義的行為。
用法
reverse_iterator rend(); //until C++ 11
const_reverse_iterator rend() const; //until C++ 11
reverse_iterator rend() nothrow; //since C++ 11
const_reverse_iterator rend() const nothrow; //since C++ 11
參數
空
返回值
它返回一個反向迭代器,指向反向容器的最後一個元素之後的元素。
複雜度
恒定。
迭代器有效性
沒有變化。
數據競爭
容器被訪問。 const 和非常量版本都不會修改容器。
異常安全
此函數從不拋出異常。
例子1
讓我們看看 rend() 函數的簡單示例:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char,int> mymultimap;
mymultimap = {
{'a', 100},
{'b', 200},
{'c', 100},
{'b', 400}
};
// show content:
multimap<char,int>::reverse_iterator rit;
for (rit=mymultimap.rbegin(); rit!=mymultimap.rend(); ++rit)
cout << rit->first << " = " << rit->second << '\n';
return 0;
}
輸出:
c = 100 b = 400 b = 200 a = 100
在上麵的例子中, rend() 函數用於返回一個反向迭代器到反向容器的最後一個元素之後的元素。
由於 multimap 以鍵的排序順序存儲元素,因此迭代 multimap 將導致上述順序,即鍵的排序順序。
例子2
讓我們看一個使用 while 循環以相反順序迭代 multimap 的簡單示例:
#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;
int main() {
// Creating & Initializing a multimap of String & Ints
multimap<string, int> multimapEx = {
{ "aaa", 10 },
{ "ddd", 11 },
{ "aaa", 12 },
{ "ccc", 13 }
};
// Create a multimap iterator and point to the end of multimap
multimap<string, int>::reverse_iterator it = multimapEx.rbegin();
// Iterate over the multimap using Iterator till beginning.
while (it != multimapEx.rend()) {
// Accessing KEY from element pointed by it.
string word = it->first;
// Accessing VALUE from element pointed by it.
int count = it->second;
cout << word << "::" << count << endl;
// Increment the Iterator to point to next entry
it++;
}
return 0;
}
輸出:
ddd::11 ccc ::13 aaa::12 aaa::10
在上麵的例子中,我們使用 while 循環以相反的順序迭代 multimap。
由於 multimap 以鍵的排序順序存儲元素,因此迭代 multimap 將導致上述順序,即鍵的排序順序。
例子3
讓我們看一個簡單的例子
#include <map>
#include <iostream>
int main( )
{
using namespace std;
multimap <int, int> m1;
multimap <int, int>::iterator m1_Iter;
multimap <int, int>::reverse_iterator m1_rIter;
multimap <int, int>::const_reverse_iterator m1_crIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );
m1_rIter = m1.rend( );
m1_rIter--;
cout << "The last element of the reversed multimap m1 is "
<< m1_rIter -> first << "." << endl;
// begin can be used to start an iteration
// through a multimap in a forward order
cout << "The multimap is:";
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end( ); m1_Iter++)
cout << m1_Iter -> first << " ";
cout << "." << endl;
// rbegin can be used to start an iteration
// throught a multimap in a reverse order
cout << "The reversed multimap is:";
for ( m1_rIter = m1.rbegin( ) ; m1_rIter != m1.rend( ); m1_rIter++)
cout << m1_rIter -> first << " ";
cout << "." << endl;
// A multimap element can be erased by dereferencing to its key
m1_rIter = --m1.rend( );
m1.erase ( m1_rIter -> first );
m1_rIter = m1.rend( );
m1_rIter--;
cout << "After the erasure, the last element "
<< "in the reversed multimap is "
<< m1_rIter -> first << "." << endl;
return 0;
}
輸出:
The last element of the reversed multimap m1 is 1. The multimap is:1 2 3 . The reversed multimap is:3 2 1 . After the erasure, the last element in the reversed multimap is 2.
在上麵的例子中,multimap 的元素以相反的順序返回。
示例 4
讓我們看一個簡單的例子來排序和計算最高工資:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
multimap<int,int> emp = {
{ 1000, 10},
{ 3500, 20 },
{ 4500, 30 },
{ 3000, 40 },
{ 3500, 50 }};
cout << "Salary" << " | " << "ID" << '\n';
cout<<"______________________\n";
multimap<int,int>::reverse_iterator rit;
for (rit=emp.rbegin(); rit!=emp.rend(); ++rit)
cout << rit->first << " | " << rit->second << '\n';
auto ite = emp.rbegin();
cout << "\nHighest salary:"<< ite->first <<" \n";
cout << "ID is:"<< ite->second << "\n";
return 0;
}
輸出:
Salary | ID ______________________ 4500 | 30 3500 | 50 3500 | 20 3000 | 40 1000 | 10 Highest salary:4500 ID is:30
在上麵的示例中,實現了一個多映射 emp,其中 ID 存儲為值,salary 存儲為鍵。這使我們能夠利用 multimaps 中的自動排序,並讓我們能夠識別具有最高薪水的元素的 ID。
相關用法
- C++ multimap rend用法及代碼示例
- C++ multimap rbegin用法及代碼示例
- C++ multimap rbegin()用法及代碼示例
- C++ multimap key_comp()用法及代碼示例
- C++ multimap empty()用法及代碼示例
- C++ multimap cend()用法及代碼示例
- C++ multimap insert()用法及代碼示例
- C++ multimap lower_bound()用法及代碼示例
- C++ multimap maxsize()用法及代碼示例
- C++ multimap get_allocator()用法及代碼示例
- C++ multimap upper_bound()用法及代碼示例
- C++ multimap clear()用法及代碼示例
- C++ multimap crend()用法及代碼示例
- C++ multimap crbegin()用法及代碼示例
- C++ multimap key_comp用法及代碼示例
- C++ multimap emplace()用法及代碼示例
- C++ multimap equal_range()用法及代碼示例
- C++ multimap value_comp()用法及代碼示例
- C++ multimap erase()用法及代碼示例
- C++ multimap size()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ multimap rend() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。