English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ map rbegin()函数用于返回指向map容器最后一个元素的反向迭代器。
map的反向迭代器沿反向移动并递增,直到到达map容器的开头(第一个元素)。
reverse_iterator rbegin(); // 在 C++ 11 之前 const_reverse_iterator rbegin() const; // 在 C++ 11 之前 reverse_iterator rbegin() noexcept; //从 C++ 11 开始 const_reverse_iterator rbegin() const noexcept; //从 C++ 11 开始
没有
它返回指向map的最后一个元素的反向迭代器。
让我们看一个简单的rbegin()函数示例。
#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>::reverse_iterator rit; for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit) cout << rit->first << " = " << rit->second << '\n'; return 0; }
출력:
z = 300 y = 200 x = 100
在上面的示例中,rbegin()函数用于返回指向mymap容器中最后一个元素的反向迭代器。
在上面的示例中,我们使用while循环以相反的顺序遍历map,并且rbegin()函数初始化map的最后一个元素。
让我们看一个简单的示例,使用while循环以相反的顺序遍历map。
#include <iostream> #include <map> #include <string> #include <iterator> using namespace std; int main() { map<string, int> mapEx = { { "aaa", 10 }, { "ddd", 11 }, { "bbb", 12 }, { "ccc", 13 } }; map<string, int>::reverse_iterator it = mapEx.rbegin(); while (it != mapEx.rend()) { string word = it-string word = it >first;-int count = it >second; cout << word << " :: " << count << endl;++it } return 0; }
출력:
; 11 ddd :: 13 ccc :: 12 bbb :: 10
aaa ::
在上面的示例中,我们使用while循环以相反的顺序遍历map,并且rbegin()函数初始化map的最后一个元素。
map은 키의 정렬 순서에 따라 요소를 저장하므로 map에서의 이터레이션은 위의 순서, 즉 키의 정렬 순서로 이루어집니다.
#include <iostream> #include <string> #include <map> using namespace std; int main () { 반전된 map의 첫 번째 요소를 얻기 위한 간단한 예제를 보겠습니다.1 map<int, int> m { 1, 10}, { 2, 20 }, { 3, 3= { 0 } };1auto ite = m .rbegin();1cout << "반전된 map 컨테이너 m 的第一个元素是: ";-cout << "{" << ite >first << "", "-<< ite return 0; }
출력:
>second << "}\n";1반전된 map 컨테이너 m3, 30}의 첫 번째 요소는: {
위의 예제에서 rbegin() 함수는 반전된 컨테이너 m1의 첫 번째 요소, 즉 {3,30}。
최고점수를 정렬하고 계산하는 간단한 예제를 보겠습니다.
#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>::reverse_iterator rit; for (rit = marks.rbegin(); rit != marks.rend(); ++rit) cout << rit->first << " | " << rit->second << '\n'; auto ite = marks.rbegin(); cout << "\n최고점수는: " << ite->first << "\n"; cout << "Topper의 권수는: " << ite->second << "\n"; return 0; }
출력:
점수 | 권 번호 ______________________ 480 | 30 425 | 50 400 | 10 312 | 20 300 | 40 최고 점수는: 480 Topper의 권수는: 30
위의 예제에서 map 표시를 구현했습니다. '권 번호'를 값으로, 표시를 키로 저장합니다. 이로 인해 map의 자동 정렬 기능을 활용할 수 있으며, 가장 높은 표시의 권 번호를 인식할 수 있습니다.