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 rbegin()、rend()用法及代码示例
- C++ multiset rbegin()用法及代码示例
- 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 rend()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
