English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ map crbegin()函数用于返回引用map容器中最后一个元素的常量反向迭代器。
常量map的反向迭代器将反向移动并递增,直到到达map容器的开头(第一个元素)并指向常量元素。
const_reverse_iterator crbegin() const noexcept; //从 C++ 11 开始
没有
它返回一个常数反向迭代器,指向map的最后一个元素。
让我们看一个简单的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:"; for (auto rit = mymap.crbegin(); rit != mymap.crend(); ++rit) cout << " [" << rit->first << ':' << rit->second << ']'; cout << '\n'; return 0; }
Saída:
以相反的顺序排列mymap: [c:300] [b:100] [a:200]
在上面的示例中,使用crbegin()函数返回一个常数反向迭代器,该迭代器指向mymap容器中的最后一个元素。
因为map按键的排序顺序存储元素。因此,在map上进行迭代将导致上述顺序,即键的排序顺序。
让我们看一个简单的示例,使用while循环以相反的顺序遍历map。
#include <iostream> #include <map> #include <string> #include <iterator> using namespace std; int main() { // 创建和初始化string和int的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()) { //从其指向的元素访问KEY。 string word = it->first; //从它所指向的元素中访问VALUE。 int count = it->second; cout << word << " :: " << count << endl; //Increase the iterator to point to the next entry it++; } return 0; }
Saída:
ddd :: 11 ccc :: 13 bbb :: 12 aaa :: 10
In the above example, we use a while loop to iterate over the map in reverse order and initialize the last element of the map using the crbegin() function.
Because the map stores elements in the order of the key's sorting, iterating over the map will result in the above order, that is, the order of the keys.
Let's see a simple example to get the first element of the reversed 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 << "Reversed map container m1The first element is: " cout << "{" << ite-first << "", " << ite-second << "}\n"; return 0; }
Saída:
The reversed map container m1The first element is: {3, 30}。
In the above example, the crbegin() function returns the first element of the reversed map container m1The first element, that is, {3,30}。
Let's see a simple example to sort and calculate the highest score.
#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 score is: " << ite-first << "\n"; cout << "Topper's volume is: " << ite->second << "\n"; return 0; }
Saída:
MARCAS | NÚMERO DE VOLUME ______________________ 480 | 30 425 | 50 400 | 10 312 | 20 300 | 40 A maior pontuação é: 480 O número de volume do Topper é: 30
No exemplo acima, foi implementado o marcador map, onde o 'Número de Volume (Roll Number)' é armazenado como valor e o marcador é armazenado como chave. Isso nos permite aproveitar a função de classificação automática do map e nos permite identificar o número de volume do elemento com o marcador mais alto.