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

📅  最后修改于: 2021-05-30 15:29:22             🧑  作者: Mango

std :: map :: rbegin()是C++ STL中的一个函数。它返回一个反向迭代器,该迭代器指向地图的最后一个元素。反向迭代器以相反的顺序进行迭代,递增迭代器意味着朝着地图的开头移动。

句法:

r_i rbegin();
const_r_i rbegin() const;

参数:它不带任何参数。

返回:返回一个反向迭代器,该迭代器指向地图的最后一个元素。

时间复杂度: O(1)

以下示例说明了map :: rbegin()方法:

范例1:

// C++ Program to illustrate
// map::rbegin() method
  
#include 
#include 
using namespace std;
  
int main()
{
  
    map mp = {
        { 'a', 1 },
        { 'b', 2 },
        { 'c', 3 },
        { 'd', 4 },
        { 'e', 5 },
    };
  
    cout << "Map contains following "
         << "elements in reverse order"
         << endl;
  
    for (auto i = mp.rbegin(); i != mp.rend(); ++i)
        cout << i->first
             << " = " << i->second
             << endl;
  
    return 0;
}
输出:
Map contains following elements in reverse order
e = 5
d = 4
c = 3
b = 2
a = 1

范例2:

// C++ Program to illustrate
// map::rbegin() method
  
#include 
#include 
using namespace std;
  
int main()
{
  
    map mp = {
        { 'a', 'A' },
        { 'b', 'B' },
        { 'c', 'C' },
        { 'd', 'D' },
        { 'e', 'E' },
    };
  
    cout << "Map contains following "
         << "elements in reverse order"
         << endl;
  
    for (auto i = mp.rbegin(); i != mp.rend(); ++i)
        cout << i->first
             << " = " << i->second
             << endl;
  
    return 0;
}
输出:
Map contains following elements in reverse order
e = E
d = D
c = C
b = B
a = A
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”