當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ multiset rend()用法及代碼示例

C++ multiset rend() 函數用於以相反的順序返回一個迭代器到multiset的末尾(不是最後一個元素而是過去的最後一個元素)。這類似於非逆向容器的第一個元素之前的元素。

注意:- 這是一個占位符。此位置不存在元素,嘗試訪問是未定義的行為。

用法

reverse_iterator rend();                             //until C++ 11

const_reverse_iterator rend() const;                 //until C++ 11

      reverse_iterator rend() nothrow;                //since C++ 11

const_reverse_iterator rend() const nothrow;    //since C++ 11

參數

返回值

它返回一個反向迭代器,指向反向容器的最後一個元素之後的元素。

複雜度

恒定。

迭代器有效性

沒有變化。

數據競爭

容器被訪問。常量和非常量版本都不會修改容器。

同時訪問多集容器的元素是安全的。

異常安全

此函數從不拋出異常。

例子1

讓我們看看 rend() 函數的簡單示例:

#include <iostream>
#include <set>

using namespace std;

int main () {
   multiset<int> mymultiset = {30,40,20,10,20};

   cout << "Elements are:";
   for (auto rit = mymultiset.rbegin(); rit != mymultiset.rend(); ++rit)
      cout << ' ' << *rit;
      cout << '\n';

   return 0;
}

輸出:

Elements are:40 30 20 20 10

在上麵的例子中, rend() 函數用於返回一個反向迭代器到反向容器的最後一個元素之後的元素。

由於多重集以鍵的排序順序存儲元素,因此迭代多重集將導致上述順序,即鍵的排序順序。

例子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 = {"aaa", "bbb", "ccc", "ddd", "ccc"};
 
    // Create a multiset iterator and point to the end of multiset
    multiset<string>::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
ccc
bbb
aaa

在上麵的例子中,我們使用 while 循環以相反的順序迭代多重集。由於多重集以鍵的排序順序存儲元素,因此迭代多重集將導致上述順序,即鍵的排序順序。

例子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( 10 );
  
   s1_rIter = s1.rend( );  
   s1_rIter--;  
   cout << "The last element in the reversed multiset is "  
        << *s1_rIter << "." << endl;  
  
   // end can be used to terminate 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;  
  
   // rend can be used to terminate 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;  
  
   s1_rIter = s1.rend( );  
   s1_rIter--;  
   s1.erase ( *s1_rIter );  
  
   s1_rIter = s1.rend( );  
   --s1_rIter;  
   cout << "After erase, the last element in the "  
        << "reversed multiset is " << *s1_rIter << "." << endl;  
}

輸出:

The last element in the reversed multiset is 10.
The multiset is:10 10 20 30 .
The reversed multiset is:30 20 10 10 .
After erase, the last element in the reversed multiset is 20.

在上麵的例子中,multiset 的元素以相反的順序返回。

示例 4

讓我們看一個簡單的例子來排序和計算最高分:

#include <iostream>
#include <string>
#include <set>

using namespace std;

int main ()
{
  multiset<int> emp = {1000,2500,4500,5000,3000,4500};

   cout << "Salary" << '\n';
   cout<<"______________________\n";
   
  multiset<int>::reverse_iterator rit;
    for (rit=emp.rbegin(); rit!=emp.rend(); ++rit)
    cout << *rit<< '\n';

    auto ite = emp.rbegin();
 
    cout << "\nHighest salary:"<< *ite <<" \n";

  return 0;
  }

輸出:

Salary
______________________
5000
4500
4500
3000
2500
1000

Highest salary:5000

在上麵的示例中,實現了一個多集 emp,其中將薪水存儲為值。這使我們能夠利用 multisets 中的自動排序,並讓我們確定最高薪水。





相關用法


注:本文由純淨天空篩選整理自 C++ multiset rend()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。