C++ Map 中的 rbegin函数

首页 / C++入门教程 / C++ Map 中的 rbegin函数

C++ map rbegin()函数用于返回引用映射集合最后一个元素的反向迭代器

映射的反向迭代器以相反的方向移动并递增,直到到达映射集合的开头(第一个元素)。

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

rbegin - 返回值

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

rbegin - 例子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映射中最后一个元素的反向迭代器。

由于map因此按键的排序顺序存储元素,因此在map上进行迭代将导致上述顺序,即键的排序顺序。

rbegin - 例子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()函数初始化映射的最后一个元素。

由于map因此按键的排序顺序存储元素,因此在map上进行迭代将导致上述顺序,即键的排序顺序。

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}。

rbegin - 例子4

让我们看一个简单的示例,对最高分进行排序和计算。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-map-rbegin-function.html

来源:LearnFk无涯教程网

#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

在上面的示例中,实现了映射标记,其中将"Roll Number"存储为值,并将标记存储为键。这使我们能够利用映射中的自动排序函数,并使我们能够识别标记最高的元素的卷号。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

React实战进阶45讲 -〔王沛〕

消息队列高手课 -〔李玥〕

罗剑锋的C++实战笔记 -〔罗剑锋〕

A/B测试从0到1 -〔张博伟〕

技术面试官识人手册 -〔熊燚(四火)〕

说透数字化转型 -〔付晓岩〕

说透5G -〔杨四昌〕

Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

好记忆不如烂笔头。留下您的足迹吧 :)