C++ Map 中的 crend函数

首页 / C++入门教程 / C++ Map 中的 crend函数

C++映射 crend()函数用于以反向顺序将常量迭代器返回到映射的末尾(不是最后一个元素,而是过去的最后一个元素) 。这类似于非反转集合的第一个元素之前的元素。

Note:-这是一个占位符。此位置没有元素,尝试访问是未定义的行为。

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

crend - 语法

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

crend - 参数

没有

crend - 返回值

它将const_reverse_iterator返回到反转集合的最后一个元素之后的元素。

无涯教程网

crend - 例子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上进行迭代将导致上述顺序,即键的排序顺序。

crend - 例子2

让我们看一个简单的示例,使用while循环以相反的顺序遍历映射。

#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上进行迭代将导致上述顺序,即键的排序顺序。

crend - 例子3

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

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-map-crend-function.html

来源:LearnFk无涯教程网

#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的元素以相反的顺序返回。

crend - 例子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作为值存储,而salary作为键存储。这使我们能够利用映射中的自动排序函数,并使我们能够确定薪水最高的元素的ID。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

邱岳的产品手记 -〔邱岳〕

Go语言从入门到实战 -〔蔡超〕

深入浅出云计算 -〔何恺铎〕

如何看懂一幅画 -〔罗桂霞〕

如何讲好一堂课 -〔薛雨〕

遗留系统现代化实战 -〔姚琪琳〕

Kubernetes入门实战课 -〔罗剑锋〕

AI大模型之美 -〔徐文浩〕

结构沟通力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)