C++ multiset crbegin() 函數用於返回引用 multiset 容器中最後一個元素的常量反向迭代器。
multiset 的常量反向迭代器以相反的方向移動並遞增它,直到它到達 multiset 容器的開頭(第一個元素)並指向常量元素。
用法
const_reverse_iterator crbegin() const noexcept; //since C++ 11
參數
空
返回值
它返回一個指向多重集最後一個元素的常量反向迭代器。
參數
空
返回值
crbegin() 函數返回一個常量反向迭代器,指向 multimap 的最後一個元素。
複雜度
恒定。
迭代器有效性
沒有變化。
數據競爭
容器被訪問。
同時訪問多集容器的元素是安全的。
異常安全
這個函數從不拋出異常。
例子1
讓我們看看 crbegin() 函數的簡單示例:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<int> mymultiset = {40,20,30,10,30,10};
cout << "mymultiset in reverse order:";
for (auto rit=mymultiset.crbegin(); rit != mymultiset.crend(); ++rit)
cout << ' ' << *rit;
cout << '\n';
return 0;
}
輸出:
mymultiset in reverse order:40 30 30 20 10 10
在上麵的例子中,crbegin() 函數用於返回一個指向 mymultiset 多集中最後一個元素的常量反向迭代器。
由於多重集以鍵的排序順序存儲元素,因此迭代多重集將導致上述順序,即鍵的排序順序。
例子2
讓我們看一個使用 while 循環以相反順序迭代多重集的簡單示例:
#include <iostream>
#include <set>
#include <string>
#include <iterator>
using namespace std;
int main() {
// Creating & Initializing a multiset of String & Ints
multiset<string> multisetEx = {"bbb", "ccc", "aaa", "bbb"};
// Create a multiset iterator and point to the end of multiset
multiset<string>::const_reverse_iterator it = multisetEx.crbegin();
// Iterate over the multiset using Iterator till beginning.
while (it != multisetEx.crend()) {
// Accessing KEY from element pointed by it.
string word = *it;
cout << word << endl;
// Increment the Iterator to point to next entry
it++;
}
return 0;
}
輸出:
ccc bbb bbb aaa
在上麵的例子中,我們以相反的順序在多重集上使用 while 循環到 const_iterate 和 crbegin() 函數初始化多重集的最後一個元素。
由於多重集以鍵的排序順序存儲元素,因此迭代多重集將導致上述順序,即鍵的排序順序。
例子3
讓我們看一個簡單的例子來獲取反向多重集的第一個元素:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
multiset<int> s1 = {20,40,10,30, 20};
auto ite = s1.crbegin();
cout << "The first element of the reversed multiset s1 is:";
cout << *ite;
return 0;
}
輸出:
The first element of the reversed multiset s1 is:40
在上麵的例子中,crbegin() 函數返回反向多重集 s1 的第一個元素,即 40。
示例 4
讓我們看一個簡單的例子來排序和計算最高分:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
multiset<int> marks = {400, 220, 250, 250, 365, 220};
cout << "Marks" << '\n';
cout<<"______________________\n";
multiset<int>::const_reverse_iterator rit;
for (rit=marks.crbegin(); rit!=marks.crend(); ++rit)
cout << *rit<< '\n';
auto ite = marks.crbegin();
cout << "\nHighest Marks is:"<< *ite<<" \n";
return 0;
}
輸出:
Marks ______________________ 400 365 250 250 220 220 Highest Marks is:400
在上麵的示例中,實現了多集 'marks',其中該多集的元素存儲為鍵。函數 crbegin() 使我們能夠利用多重集中的自動排序,並讓我們識別最高分。
相關用法
- C++ multiset crbegin()、crend()用法及代碼示例
- C++ multiset crend()用法及代碼示例
- C++ multiset count()用法及代碼示例
- C++ multiset cbegin()用法及代碼示例
- C++ multiset cbegin()、cend()用法及代碼示例
- C++ multiset cend()用法及代碼示例
- C++ multiset clear()用法及代碼示例
- C++ multiset begin()、end()用法及代碼示例
- C++ multiset value_comp()用法及代碼示例
- C++ multiset emplace()用法及代碼示例
- C++ multiset lower_bound()用法及代碼示例
- C++ multiset insert()用法及代碼示例
- C++ multiset max_size()用法及代碼示例
- C++ multiset rbegin()、rend()用法及代碼示例
- C++ multiset find()用法及代碼示例
- C++ multiset begin()用法及代碼示例
- C++ multiset size()用法及代碼示例
- C++ multiset get_allocator()用法及代碼示例
- C++ multiset empty()用法及代碼示例
- C++ multiset rend()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ multiset crbegin()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。