C++ set rend()函数用于以相反的顺序将迭代器返回到集合的末尾(不是最后一个元素,而是过去的最后一个元素)。这类似于非反转集合的第一个元素之前的元素。
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
它将反向迭代器返回到反转集合最后一个元素之后的元素。
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-set-rend-function.html
来源:LearnFk无涯教程网
让我们看一下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()函数用于将反向迭代器返回到反向集合后一个元素之后的元素。
因为集合因此按键的排序顺序存储元素,所以对集合进行迭代将导致上述顺序,即键的排序顺序。
让我们看一个简单的示例,使用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循环以相反的顺序遍历集合。
因为集合因此按键的排序顺序存储元素,所以对集合进行迭代将导致上述顺序,即键的排序顺序。
让我们看一个简单的例子:
#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的元素以相反的顺序返回。
让我们看一个简单的示例来对最高分进行排序和计算:
#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设置,其中ID被存储为值,salary作为键。这使我们能够利用集合中的自动排序函数,并使我们能够识别薪水最高的元素的ID。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)