C++多重映射 rend()函数用于以相反的顺序将迭代器返回到多重映射的末尾(不是最后一个元素,而是过去的最后一个元素)。与非反转集合的第一个元素之前的元素相似。
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
它将反向迭代器返回到反转集合最后一个元素之后的元素。
让我们看一下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上进行迭代将导致上述顺序,即键的排序顺序。
让我们看一个简单的示例,使用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。
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-multimap-rend-function.html
来源:LearnFk无涯教程网
由于multimap因此按键的排序顺序存储元素,因此在multimap上进行迭代将导致上述顺序,即键的排序顺序。
让我们看一个简单的例子
#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的元素以相反的顺序返回。
让我们看一个简单的例子排序并计算最高薪水:
#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
在上面的示例中,实现了一个multimap emp,其中ID被存储为值,salary作为键。这使我们能够利用多映射中的自动排序函数,并使我们能够确定薪水最高的元素的ID。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)