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

C++ multimap rbegin()

C++ multimap rbegin()函数

C++ multimap rbegin()函数用于返回反向迭代器引用
multimap 的反向迭代器沿相反方向移动并递增,直到到达 multimap 容器的开始(第一个元素)为止。

语法

reverse_iterator rbegin();                            //until C++ 11
const_reverse_iterator rbegin() const;                //until C++ 11
      reverse_iterator rbegin() nothrow;          //since C++ 11
const_reverse_iterator rbegin() const nothrow;    //since C++ 11

参数

返回值

它返回一个指向其最后一个元素的反向迭代器

复杂度

常量。

迭代器有效性

没有变化。

数据竞争

无论是const版本还是非const版本都不能访问该容器。

异常安全

此函数从不抛出异常。

示例1

让我们看一下rbegin()函数的简单示例:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
  multimap<char,int> mymultimap;
  mymultimap = {
               {'a', 100},
               {'b', 200},
               {'a', 300},
               {'c', 300}
               };
  // 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 = 300
b = 200
a = 300
a = 100
在上面的示例中,rbegin()函数用于返回指向mymultimap set 映射中最后一个元素的反向迭代器。
因为 set 映射因此按键的排序顺序存储元素,所以进行了迭代

示例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 },
            { "ccc", 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
ccc :: 12
aaa :: 10
在上面的示例中,我们使用while循环以相反的顺序迭代 multimap ,并使用rbegin()函数初始化 multimap 的最后一个元素。
因为 multimap 按排序顺序存储元素因此,遍历多个映射将按上述顺序进行操作,即按键的排序顺序。

示例3

让我们看一个简单的示例来获取第一个元素逆向 multimap :
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  multimap<int,int> m1 = {
                { 1, 10},
                { 2, 20 },
                { 3, 30 }, 
                { 3, 40 },
                { 4, 50}
                };         
    auto ite = m1.rbegin();
    cout << "The first element of the reversed multimap m1 is: ";
    cout << "{" << ite->first << ", "
         << ite->second << "}\n";
  return 0;
  }  
输出:
The first element of the reversed multimap m1 is: {4, 50}
在上面的示例中,rbegin()函数返回反转的 set 映射m1的第一个元素,即{4,50}。

示例4

让我们看一个简单的示例来对最高分进行排序和计算:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  multimap<int,int> marks = {
                { 425, 10},
                { 300, 20 },
                { 480, 30 },
                { 300, 40 },
                { 425, 50 }};
   cout << "Marks" << " | " << "Roll Number" << '\n';
   cout<<"______________________\n";
  multimap<int,int>::reverse_iterator rit;
  for (rit=marks.rbegin(); rit!=marks.rend(); ++rit)
    cout << rit->first << "   |  " << rit->second << '\n';
    auto ite = marks.rbegin();
    cout << "\nHighest Marks is: "<< ite->first <<" \n";
    cout << "Roll Number of Topper is: "<< ite->second << "\n";
  return 0;
  }
输出:
Marks | Roll Number
______________________
480   |  30
425   |  50
425   |  10
300   |  40
300   |  20
Highest Marks is: 480 
Roll Number of Topper is: 30
在上面的示例中,实现了 multimap 标记,其中,卷号存储为值,标记为键。这使我们能够利用多地图中的自动排序功能,并让我们识别出标记最高的元素的滚动编号。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4