首页 文章资讯内容详情

在C ++ STL中映射rbegin()函数

2026-06-04 1 花语

在本文中,我们将讨论C++STL中map::rbegin()函数的工作,语法和示例。

什么是C++STL中的映射?

映射是关联容器,它有助于按特定顺序存储由键值和映射值的组合形成的元素。在映射容器中,数据始终在内部借助其关联的键进行排序。映射容器中的值通过其唯一键访问。

什么是map::rbegin()?

map::rbegin()函数是C++STL中的内置函数,该函数在 headerfile.rbegin()impliesreversebeginfunction,thisfunctionisthereverseofthebegin().Thisfunctionreturnsaniteratorwhichispointingtothelastelementofthemapcontainer.

语法

Map_name.rbegin();

参数

此函数不接受任何参数。

返回值

此函数返回指向映射容器最后一个元素的迭代器。

示例

输入项

map<char, int> newmap; newmap[‘a’] = 1; newmap[‘b’] = 2; newmap[‘c’] = 3; newmap.rbegin();

输出结果

c:3

示例

#include <bits/stdc++.h> using namespace std; int main() { map<int, int> TP_Map; TP_Map.insert({3, 50}); TP_Map.insert({2, 30}); TP_Map.insert({1, 10}); TP_Map.insert({4, 70}); //using map::rbegin to fetch first last element auto temp = TP_Map.rbegin(); cout<<"First element is: "<<temp->first << " -> " << temp->second; cout<<"\nTP Map is : \n"; cout << "MAP_KEY\tMAP_ELEMENT\n"; for (auto i = TP_Map.rbegin(); i!= TP_Map.rend(); i++) { cout << i->first << "\t" << i->second << endl; } return 0; }

输出结果

First element is: 4 -> 70 TP Map is: MAP_KEY MAP_ELEMENT 4 70 3 50 2 30 1 10