在 C++ STL 中的多重映射 rend
在本文中,我们将讨论 C++ STL 中多重映射::rend() 函数的工作原理、语法和示例。
什么是 C++ STL 中的多重映射?
多重映射是关联容器,类似于 map 容器。它还有助于按特定顺序存储由键值和映射值组合形成的元素。在多重映射容器中,有多个元素可以关联到同一键。数据在内部总是借助其关联键进行排序。
什么是 multimap::rend()?
multimap::rend() 函数是 C++ STL 中的一个内置函数,定义在
语法
multiMap_name.rend();
参数
此函数不接受任何参数。
返回值
此函数返回指向 multimap 容器最后一个元素的迭代器。
输入
multimap<char, int> newmap; newmap[‘a’] = 1; newmap[‘b’] = 2; newmap[‘c’] = 3; newmap.rend();
输出
error
示例
#include <bits/stdc++.h>
using namespace std;
int main(){
multimap<int, int> mul;
//inserting elements in multimap
mul.insert({ 1, 10 });
mul.insert({ 2, 20 });
mul.insert({ 3, 30 });
mul.insert({ 4, 40 });
mul.insert({ 5, 50 });
//displaying multimap elements
cout << "\nElements in multimap is : \n";
cout << "KEY\tELEMENT\n";
for (auto it = mul.rbegin(); it!= mul.rend(); ++it){
cout << it->first << '\t' << it->second << '\n';
}
return 0;
}输出
如果我们运行以上代码,它将生成以下输出 -
Elements in multimap is : KEY ELEMENT 5 50 4 40 3 30 2 20 1 10
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP