English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ set crendLa funzione viene utilizzata perordine inversoConIteratore costanteRestituisce all'ultimo elemento del set (dopo l'ultimo elemento). È simile all'elemento prima del primo elemento di un contenitore non inverso.
const_reverse_iterator crend() const noexcept; // Da C++ 11
Nessuno
Restituisce un const_reverse_iterator alla fine del contenitore in senso inverso.
Immutabile.
Nessuna modifica.
Il contenitore viene acceduto.
L'accesso contemporaneo agli elementi della集合 è sicuro.
Questa funzione non solleva mai eccezioni.
Vediamo un esempio semplice della funzione crend():
#include <iostream> #include <set> using namespace std; int main () { set<int> myset = {40, 20, 50, 10, 30}; cout << "myset in ordine inverso:"; for (auto rit = myset.crbegin(); rit != myset.crend(); ++rit) cout << ' ' << *rit; cout << '\n'; return 0; }
Output:
myset in ordine inverso: 50 40 30 20 10
Nell'esempio precedente, l'uso della funzione crend() restituisce l'iteratore inverso alla fine del contenitore in senso inverso.
因为set按键的排序顺序存储元素,所以对set进行迭代将导致上述顺序,即键的排序顺序。
让我们看一个简单的示例,使用while循环以相反的顺序遍历集合:
#include <iostream> #include <set> #include <string> #include <iterator> using namespace std; int main () { // 创建和初始化一组字符串& int set<string> setEx = {"ccc", "ddd", "aaa", "bbb"}; // 创建一个set迭代器并指向set的末尾 set<string>::const_reverse_iterator it = setEx.crbegin(); // 使用迭代器遍历集合直到开始。 while (it != setEx.crend()) { // 从它所指向的元素访问键。 string word = *it; cout << word << endl; // 递增迭代器以指向下一个条目 it++; } return 0; }
Output:
ddd ccc bbb aaa
在上面的示例中,我们使用while循环以相反的顺序对集合进行const_iterate。
因为set按键的排序顺序存储元素,所以对set进行迭代将导致上述顺序,即键的排序顺序。
让我们看一个简单的实例:
#include <iostream> #include <set> #include <algorithm> using namespace std; int main () { set<int> c = {3, 1, 2}; for_each(c.crbegin(), c.crend(), [](const int& x) { cout << x << endl; }); }
Output:
3 2 1
在上面的示例中,set的元素以相反的顺序返回。
让我们看一个简单的示例来对最高分进行排序和计算:
#include <iostream> #include <string> #include <set> using namespace std; int main () { set<int> emp = {1000, 2500, 4500, 1200, 3000}; cout << "薪水" << '\n'; cout << "______________________\n"; set<int>::const_reverse_iterator rit; for (rit = emp.crbegin(); rit != emp.crend(); ++rit) cout << *rit << '\n'; auto ite = emp.crbegin(); cout << "\nStipendio più alto: " << *ite << " \n"; return 0; }
Output:
Stipendio ______________________ 4500 3000 2500 1200 1000 Stipendio più alto: 4500
In esempio sopra, è stata implementata una集合set emp, in cui lo stipendio viene memorizzato come chiave. Questo ci permette di utilizzare la funzione di ordinamento automatico dello stipendio e determinare lo stipendio più alto.