C++ map crbegin()函数

C++ map crbegin()函数

C++ map crbegin() 函数用于返回指向映射容器中的最后一个元素的常量反向迭代器。

映射容器的常量反向迭代器以相反的方向移动,并递增它,直到它达到映射容器的开头(第一个元素),并指向 constant 元素。

语法

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

参数

返回值

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

示例1

让我们看一个crbegin()函数的简单示例。

#include <iostream>
#include <map>

using namespace std;

int main ()
{
  map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  cout << "mymap in reverse order:";
  for (auto rit = mymap.crbegin(); rit != mymap.crend(); ++rit)
    cout << " [" << rit->first << ':' << rit->second << ']';
  cout << '\n';

  return 0;
}

输出:

mymap in reverse order: [c:300] [b:100] [a:200]

在上面的示例中,crbegin()函数被用来返回一个指向mymap映射中最后一个元素的常量反向迭代器。

因为map按照键的排序顺序来存储元素。因此,遍历map将以上述顺序进行,即按照键的排序顺序。

示例2

让我们来看一个简单的示例,使用while循环以逆序遍历map。

#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>::const_reverse_iterator it = mapEx.crbegin();

    // Iterate over the map using Iterator till beginning.
    while (it != mapEx.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
bbb :: 12
aaa :: 10

在上面的示例中,我们使用while循环以逆序方式const_iterate遍历map,使用crbegin()函数初始化map的最后一个元素。

由于map按键的排序顺序存储元素,因此遍历map将得到上述顺序,即键的排序顺序。

示例3

让我们看一个简单的示例,获取反转map的第一个元素。

#include <iostream>
#include <string>
#include <map>


using namespace std;

int main ()
{
  map<int,int> m1 = {
                { 1, 10},
                { 2, 20 },
                { 3, 30 } };

    auto ite = m1.crbegin();

    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}

在上面的示例中,crbegin()函数返回反转的map m1的第一个元素,即{3,30}。

示例4

让我们看一个简单的示例来对成绩进行排序并计算最高分。

#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>::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   | 30
425   | 50
400   | 10
312   | 20
300   | 40

Highest Marks is: 480 
Roll Number of Topper is: 30

在上面的示例中,实现了一个地图标记,其中将Roll Number存储为值,将成绩存储为键。这使我们能够利用地图中的自动排序,让我们能够识别具有最高分数的元素的Roll号码。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程