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