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

C++ multiset rbegin()

C++ multiset rbegin()

C++ multiset rbegin()函数用于返回指向 multiset 容器最后一个元素的反向迭代器。
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

参数

返回值

它返回反向的迭代器(reverse iterator)指向 multiset 的最后一个元素。

复杂度

常量。

迭代器有效性

没有更改。

数据竞争

无论是非常量版本还是const版本都不能修改 multiset 容器,因此不能访问 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 multiset 中的最后一个元素的反向迭代器。
因为 multiset 因此按键的排序顺序存储元素,因此要进行迭代

示例2

让我们看一个简单的示例,使用while逆序遍历 multiset 。循环:
#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循环以相反的顺序迭代 multiset ,并使用rbegin()函数初始化 multiset 的最后一个元素。
因为 multiset 按排序顺序存储元素因此,对一个键集进行迭代将导致上述顺序,即键的排序顺序。

示例3

让我们看一个简单的示例来获取第一个元素反转的 multiset :
#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 
在上面的示例中,实现了 multiset 标记,其中标记为键。这使我们能够利用 multiset 中的自动排序功能,并让我们识别最高分。 ,
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4