C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ multimap rend()

C++ multimap rend()函数

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

参数

返回值

它将反向迭代器返回到最后一个元素之后的元素

复杂度

常量。

迭代器有效性

否更改。

数据竞争

已访问容器。

异常安全

此函数从不抛出异常。

示例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()函数用于将反向迭代器返回到反转容器的最后一个元素之后的元素。
因为 set 映射因此按键的排序顺序存储元素,则在 multimap 上进行迭代将导致上述顺序,即键的排序顺序。

示例2

让我们看一个简单的示例,以相反的顺序在 multimap 上进行迭代使用while循环:
#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
在上面的示例中,实现了一个multimap emp,其中,ID作为值存储,而薪水作为键存储。这使我们能够利用多地图中的自动排序功能,并使我们能够确定薪水最高的元素的ID。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4