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

C++ multimap crbegin()

C++ multimap crbegin()函数

C++ multimap crbegin()函数用于返回常量反向迭代器,该迭代器引用了多地图容器。
多地图的恒定反向迭代器沿相反方向移动并递增,直到到达多地图容器的开头(第一个元素)并指向常量元素。

语法

const_reverse_iterator crbegin() const noexcept;//since C++ 11

参数

返回值

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

复杂度

常量。

迭代器有效性

没有变化。

数据竞争

已访问容器。

异常安全

此函数从不

示例1

让我们看一下crbegin()函数的简单示例:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
  multimap<char,string> mymultimap;
  mymultimap = { 
               {'a',"Java"},
               {'b', "C++"},
               {'b', "Python"},
               {'a', "Android"}
               };
  cout << "mymultimap in reverse order:";
  for (auto rit = mymultimap.crbegin(); rit != mymultimap.crend(); ++rit)
    cout << " [" << rit->first << ':' << rit->second << ']';
    cout << '\n';
  return 0;
}
输出:
mymultimap in reverse order: [b:Python] [b:C++] [a:Android] [a:Java]
在上面的示例中,使用crbegin()函数返回一个常数反向迭代器,该迭代器指向mymultimap multimap 中的最后一个元素。
因为 multimap 按键的排序顺序存储元素,在 multimap 上进行迭代将导致上述顺序,即键的排序顺序。

示例2

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

示例3

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

示例4

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