C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ multiset rend()

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

参数

返回值

它将反向迭代器返回到最后一个元素之后的元素

复杂度

常量。

迭代器有效性

否更改。

数据竞争

已访问容器。常量版本和非常量版本都不会修改容器。
同时访问 multiset 容器的元素是安全的。

异常安全性

此函数绝不会引发异常。

示例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()函数用于将反向迭代器返回到反向容器后最后一个元素之后的元素。
因为 multiset 会按键的排序顺序存储元素,对一个 multiset 进行迭代将导致上述顺序,即键的排序顺序。

示例2

让我们看一个简单的示例,以一个相反的顺序对 multiset 进行迭代使用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循环以相反的顺序遍历 multiset 。因为 multiset 会按键的排序顺序存储元素,所以对 multiset 进行迭代将导致上述顺序,即键的排序顺序。

示例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
在上面的示例中,实现了 multiset emp,其中薪水存储为值。这使我们能够利用 multiset 中的自动排序功能,并确定最高薪水。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4