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

C++ Set rend()

C++ Set rend()

C++ Set rend()函数用于将迭代器返回到set的末尾(而不是最后一个元素)但过去的最后一个元素)以相反的顺序。这类似于非反转容器的第一个元素之前的元素。
注意: 注意:-这是一个占位符。此位置没有元素,尝试访问是未定义的行为。

语法

      reverse_iterator rend();                             //until C++ 11
const_reverse_iterator rend() const;                 //until C++ 11
      reverse_iterator rend() noexcept;               //since C++ 11
const_reverse_iterator rend() const noexcept;   //since C++ 11

参数

返回值

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

复杂度

常量。

迭代器有效性

否更改。

数据竞争

已访问容器。 const和非const版本都不会修改容器。
同时访问集合的元素是安全的。

异常安全性

此函数永远不会引发异常。

示例1

让我们看一下rend()函数的简单示例:
#include <iostream>
#include <set>
using namespace std;
int main () {
   set<int> myset = {40,50,20,10,30};
   cout << "Elements are :";
   for (auto rit = myset.rbegin(); rit != myset.rend(); ++rit)
      cout << ' ' << *rit;
      cout << '\n';
   return 0;
}
输出:
Elements are : 50 40 30 20 10
在上面的示例中,rend()函数用于将反向迭代器返回到反转容器的最后一个元素之后的元素。
因为set按键的排序顺序存储元素,因此,对集合进行迭代将按上述顺序进行,即键的排序顺序。

示例2

让我们看一个简单的示例,以相反的顺序对集合进行迭代使用while循环:
#include <iostream>
#include <set>
#include <string>
#include <iterator>
using namespace std;
 
int main() {
 
  // Creating & Initializing a set of String & Ints
  set<string> setEx = {"aaa", "bbb", "ccc", "ddd"};
 
  // Create a set iterator and point to the end of set
  set<string>::reverse_iterator it = setEx.rbegin();
 
  // Iterate over the set using Iterator till beginning.
  while (it != setEx.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
aaa
在上面的示例中,我们使用while循环以相反的顺序迭代集合。
由于set以键的排序顺序存储元素,因此,对集合进行迭代将导致顺序,即键的排序顺序。

示例3

让我们看一个简单的示例:
#include <set>  
#include <iostream>  
  
int main() {  
   using namespace std;     
   set <int> s1;  
   set <int>::iterator s1_Iter;  
   set <int>::reverse_iterator s1_rIter;  
   s1.insert( 10 );  
   s1.insert( 20 );  
   s1.insert( 30 );  
  
   s1_rIter = s1.rend( );  
   s1_rIter--;  
   cout << "The last element in the reversed set is "  
        << *s1_rIter << "." << endl;  
  
   // end can be used to terminate an iteration   
   // throught a set in a forward order  
   cout << "The set 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 set in a reverse order  
   cout << "The reversed set 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 set is " << *s1_rIter << "." << endl;  
}  
输出:
The last element in the reversed set is 10.
The set is: 10 20 30 .
The reversed set is: 30 20 10 .
After erase, the last element in the reversed set is 20.
在上面的示例中,set的元素以相反的顺序返回。

示例4

让我们看一个简单的示例来对最高分进行排序和计算:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
  set<int> emp = {1000,2500,4500,5000,3000};
   cout << "Salary" << '\n';
   cout<<"______________________\n";
   
  set<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
3000
2500
1000
Highest salary: 5000
在上面的示例中,实现了一个emp Set ,其中ID存储为值,而薪水存储为键。这使我们能够利用集合中的自动排序功能,并使我们能够确定薪水最高的元素的ID。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4