C++ map crend()函数

C++ map crend()函数

C++ map crend() 函数用于返回一个 常量迭代器 ,其指向映射的末尾(不是最后一个元素,而是最后一个元素的下一个位置),且以 逆序 遍历。这类似于非逆序容器中的第一个元素的前一个元素。

注意:-这是一个占位符。该位置不存在元素,访问该位置会导致未定义行为。

注意:常量迭代器指的是指向常量内容的迭代器。

语法

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

参数

返回值

它返回一个const_reverse_iterator,指向反转容器中最后一个元素之后的元素。

示例1

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

#include <iostream>
#include <map>

using namespace std;

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

  mymap['x'] = 100;
  mymap['y'] = 200;
  mymap['z'] = 300;

  // show content:
  map<char,int>::const_reverse_iterator rit;
  for (rit=mymap.crbegin(); rit!=mymap.crend(); ++rit)
    cout << rit->first << " = " << rit->second << '\n';

  return 0;
}

输出:

z = 300
y = 200
x = 100

在上面的示例中,使用crend()函数返回一个常量的反向迭代器,该迭代器指向反向容器中的最后一个元素的后面一个元素。

由于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。

由于map以键的排序顺序存储元素,因此对map进行迭代将导致上述顺序,即键的排序顺序。

示例3

让我们看一个简单的示例。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   /* Initializer_list constructor */
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   cout << "Map contains following elements in reverse order:" << endl;

   for (auto it = m.crbegin(); it != m.crend(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

输出:

Map contains following elements in reverse order:
e = 5
d = 4
c = 3
b = 2
a = 1

在上面的示例中,返回的map元素按相反的顺序排列。

示例4

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

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


using namespace std;

int main ()
{
  map<int,int> emp = {
                { 1000, 10},
                { 2500, 20 },
                { 4500, 30 },
                { 3000, 40 },
                { 5500, 50 }};


   cout << "Salary" << " | " << "ID" << '\n';
   cout<<"______________________\n";

  map<int,int>::const_reverse_iterator rit;
  for (rit=emp.crbegin(); rit!=emp.crend(); ++rit)
    cout << rit->first << "   |  " << rit->second << '\n';

    auto ite = emp.crbegin();

    cout << "\nHighest salary: "<< ite->first <<" \n";
    cout << "ID is: "<< ite->second << "\n";

  return 0;
  }

输出:

Salary | ID
______________________
5500   | 50
4500   | 30
3000   | 40
2500   | 20
1000   | 10

Highest salary: 5500 
ID is: 50

在上面的示例中,实现了一个名为emp的映射,其中将ID存储为值,将薪水存储为键。这使我们能够利用映射中的自动排序,从而可以确定具有最高薪水的元素的ID。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程