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

C++ Map rbegin()

C++ Map rbegin()函数

C++映射 rbegin()函数用于返回反向迭代器地图容器的最后一个元素。
地图的反向迭代器沿相反方向移动并递增,直到到达地图容器的开头(第一个元素)。

语法

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

参数

返回值

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

示例1

我们来看一个简单的rbegin()函数示例。
#include <iostream>
#include <map>
using namespace std;
int main ()
{
  map<char,int> mymap;
  
  mymap['x'] = 100;
  mymap['y'] = 200;
  mymap['z'] = 300;
  // show content:
  map<char,int>::reverse_iterator rit;
  for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
    cout << rit->first << " = " << rit->second << '\n';
  return 0;
}
输出:
z = 300
y = 200
x = 100
在上面的示例中,rbegin()函数用于返回指向mymap映射中最后一个元素的反向迭代器。
因为映射因此按键的排序顺序存储元素,所以进行了迭代

示例2

让我们看一个简单的示例,该示例使用while反向顺序遍历该地图。循环。
#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;
 
int main() {
 
    // Creating & Initializing a map of String & Ints
    map<string, int> mapEx = {
            { "aaa", 10 },
            { "ddd", 11 },
            { "bbb", 12 },
            { "ccc", 13 }
    };
 
    // Create a map iterator and point to the end of map
    map<string, int>::reverse_iterator it = mapEx.rbegin();
 
    // Iterate over the map using Iterator till beginning.
    while (it != mapEx.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
bbb :: 12
aaa :: 10
在上面的示例中,我们使用while循环以相反的顺序遍历地图,并且rbegin()函数初始化地图的最后一个元素。
因为地图按已排序的顺序存储元素因此,遍历一个映射将按上述顺序进行操作,即按键的排序顺序。

示例3

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

示例4

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