C++ multimap crbegin()函数用于返回一个常量反向迭代器,该迭代器引用multimap集合中的最后一个元素。
multimap的恒定反向迭代器沿相反方向移动并递增,直到到达multimap集合的开头(第一个元素)并指向 constant 元素。
const_reverse_iterator crbegin() const noexcept;//since C++ 11
它返回一个常数反向迭代器,该迭代器指向multimap的最后一个元素。
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-multimap-crbegin-function.html
来源:LearnFk无涯教程网
让我们看一下crbegin()函数的简单示例:
#include <iostream> #include <map> using namespace std; int main () { multimap<char,string> mymultimap; mymultimap = { {'a',"Java"}, {'b', "C++"}, {'b', "Python"}, {'a', "Android"} }; cout << "mymultimap in reverse order:"; for (auto rit = mymultimap.crbegin(); rit != mymultimap.crend(); ++rit) cout << " [" << rit->first << ':' << rit->second << ']'; cout << '\n'; return 0; }
输出:
mymultimap in reverse order: [b:Python] [b:C++] [a:Android] [a:Java]
在上面的示例中,使用crbegin()函数返回一个常数反向迭代器,该迭代器指向mymultimap多重映射中的最后一个元素。
由于multimap因此按键的排序顺序存储元素,因此在multimap上进行迭代将导致上述顺序,即键的排序顺序。
让我们看一个简单的示例,使用while循环以相反的顺序迭代multimap:
#include <iostream> #include <map> #include <string> #include <iterator> using namespace std; int main() { // Creating & Initializing a multimap of String & Ints multimap<string, int> multimapEx = { { "aaa", 10 }, { "ddd", 11 }, { "aaa", 12 }, { "ccc", 13 } }; // Create a multimap iterator and point to the end of multimap multimap<string, int>::const_reverse_iterator it = multimapEx.crbegin(); // Iterate over the multimap using Iterator till beginning. while (it != multimapEx.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 aaa ::12 aaa ::10
在上面的示例中,我们使用while循环以相反的顺序对multimap进行const_iterate,并使用crbegin()函数初始化multimap的最后一个元素。
由于multimap因此按键的排序顺序存储元素,因此在multimap上进行迭代将导致上述顺序,即键的排序顺序。
让我们看一个简单的示例,以获取反向multimap的第一个元素:
#include <iostream> #include <string> #include <map> using namespace std; int main () { multimap<int,int> m1 = { { 1, 10}, { 2, 20 }, { 2, 30 } }; auto ite = m1.crbegin(); cout << "The first element of the reversed multimap m1 is: "; cout << "{" << ite->first << ", " << ite->second << "}\n"; return 0; }
输出:
The first element of the reversed multimap m1 is: {2, 30}
在上面的示例中,crbegin()函数返回反转的multimapm1的第一个元素,即{2,30}。
让我们看一个简单的示例来对最高分进行排序和计算:
#include <iostream> #include <string> #include <map> using namespace std; int main () { multimap<int,int> marks = { { 400, 10}, { 300, 20 }, { 400, 30 }, { 300, 40 }, { 480, 50 }}; cout << "Marks" << " | " << "Roll Number" << '\n'; cout<<"______________________\n"; multimap<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 Marks is: "<< ite->first <<" \n"; cout << "Roll Number of Topper is: "<< ite->second << "\n"; return 0; }
输出:
Marks | Roll Number _____________________ 480 | 50 400 | 30 400 | 10 300 | 40 300 | 20 Highest Marks is: 480 Roll Number of Topper is: 50
在上面的示例中,实现了一个multimap"marks",其中将卷号存储为值,并将标记存储为键。这使我们能够利用多映射中的自动排序函数,并使我们能够识别具有最高标记的元素的卷数。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)