English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ 参考手册

C++ STL map(컨테이너)

C ++ map crend() 函数使用方法及示例 mapcrend()函数用于将常量迭代器相反的顺序

返回到map的末尾(返回一个常量反向迭代器,指向第一个元素之前虚拟的元素)。这类似于非反转容器的第一个元素之前的元素。-注意:
这是一个占位符。此位置没有元素,尝试访问是未定义的行为。

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

语法 //const_reverse_iterator crend() const noexcept;++ 11 从 C

参数

没有

返回值

它将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;
  // 展示内容:
  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() {
 
	//创建和初始化String和Ints的map
	map<string, int> mapEx = {
			{ "aaa", 10 },
			{ "ddd", 11 },
			{ "bbb", 12 },
			{ "ccc", 13 }
	};
 
	//创建一个map迭代器并指向map的末尾
	map<string, int>::const_reverse_iterator it = mapEx.crbegin();
 
	//使用Iterator迭代map直到开始。
	while (it != mapEx.crend()) {
		string word = it->first;
		int count = it->second;
		cout << word << " :: " << count << endl;
		it++;
	}
	return 0;
}

출력:

ddd :: 11
ccc :: 13
bbb :: 12	
aaa :: 10

위 예제에서 while 루프를 사용하여 map을 역순으로 const_iterate합니다.

map은 키의 정렬 순서에 따라 요소를 저장하므로 map에서의 이터레이션은 키의 정렬 순서와 같은 순서로 진행됩니다.

실제 예제3

간단한 예제를 보겠습니다.

#include <iostream>
#include <map>
using namespace std;
int main(void) {
   /* Initializer_list 구조 함수 */
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };
   cout << "Map은 다음과 같은 요소를 역순으로 포함합니다:" << endl;
   for (auto it = m.crbegin(); it != m.crend(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

출력:

Map은 다음과 같은 요소를 역순으로 포함합니다:
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 << "급여 " << " | " << "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 << "\n최고 급여: " << ite->first <<" \n";
    cout << "ID is: "<< ite->second << "\n";
  return 0;
  }

출력:

급여 | ID
______________________
5500 | 50
4500 | 30
3000 | 40
2500 | 20
1000 | 10
최고 급여: 5500 
ID is: 50

위의 예제에서는 map 컨테이너 emp를 구현했으며, ID를 값으로 저장하고, 급여를 키로 저장했습니다. 이를 통해 map의 자동 정렬 기능을 활용할 수 있으며, 가장 높은 급여의 요소의 ID를 확인할 수 있습니다.

C++ STL map(컨테이너)