C++ 多映射 crbegin() 函數用於返回引用多映射容器中最後一個元素的常量反向迭代器。
multimap 的常量反向迭代器以相反的方向移動並遞增它,直到它到達 multimap 容器的開頭(第一個元素)並指向常量元素。
用法
const_reverse_iterator crbegin() const noexcept;//since C++ 11
參數
空
返回值
它返回一個指向 multimap 的最後一個元素的常量反向迭代器。
複雜度
恒定。
迭代器有效性
沒有變化。
數據競爭
容器被訪問。
異常安全
這個函數從不拋出異常。
例子1
讓我們看看 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 將導致上述順序,即鍵的排序順序。
例子2
讓我們看一個使用 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
在上麵的例子中,我們以相反的順序在 multimap 上使用 while 循環到 const_iterate 和 crbegin() 函數初始化 multimap 的最後一個元素。
由於 multimap 以鍵的排序順序存儲元素,因此迭代 multimap 將導致上述順序,即鍵的排序順序。
例子3
讓我們看一個簡單的例子來獲取反向多重映射的第一個元素:
#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() 函數返回反向多映射 m1 的第一個元素,即 {2,30}。
示例 4
讓我們看一個簡單的例子來排序和計算最高分:
#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
在上麵的例子中,實現了一個多映射 "marks",其中 Roll Number 被存儲為 value 並標記為 key。這使我們能夠利用多映射中的自動排序,並讓我們識別具有最高標記的元素的滾動數。
相關用法
- C++ multimap crend()用法及代碼示例
- C++ multimap cend()用法及代碼示例
- C++ multimap clear()用法及代碼示例
- C++ multimap cbegin()用法及代碼示例
- C++ multimap key_comp()用法及代碼示例
- C++ multimap empty()用法及代碼示例
- C++ multimap insert()用法及代碼示例
- C++ multimap lower_bound()用法及代碼示例
- C++ multimap rend()用法及代碼示例
- C++ multimap maxsize()用法及代碼示例
- C++ multimap rend用法及代碼示例
- C++ multimap get_allocator()用法及代碼示例
- C++ multimap upper_bound()用法及代碼示例
- C++ multimap key_comp用法及代碼示例
- C++ multimap emplace()用法及代碼示例
- C++ multimap equal_range()用法及代碼示例
- C++ multimap rbegin用法及代碼示例
- C++ multimap rbegin()用法及代碼示例
- C++ multimap value_comp()用法及代碼示例
- C++ multimap erase()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ multimap crbegin() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。