Zemljevid C++ render() funkcija se uporablja za vrnitev iteratorja na konec zemljevida (ne zadnji element, ampak pretekli zadnji element) v obratni vrstni red . To je podobno elementu pred prvim elementom neobrnjenega vsebnika.
Opomba:- To je ograda. Na tej lokaciji ne obstaja noben element in poskus dostopa je nedefinirano vedenje.
Sintaksa
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
Parameter
Noben
Povratna vrednost
Vrne povratni iterator elementu, ki sledi zadnjemu elementu obrnjenega vsebnika.
Primer 1
Oglejmo si preprost primer za funkcijo rend():
#include #include using namespace std; int main () { map mymap; mymap['x'] = 100; mymap['y'] = 200; mymap['z'] = 300; // show content: map::reverse_iterator rit; for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit) cout <first << '=" <second << " '; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> z = 300 y = 200 x = 100 </pre> <p>In the above example, rend() function is used to return a reverse iterator to the element following the last element of the reversed container.</p> <p>Because map store the elements in sorted order of keys therefore, iterating over a map will result in above order i.e. sorted order of keys.</p> <h2>Example 2</h2> <p>Let's see a simple example to iterate over the map in reverse order using while loop:</p> <pre> #include #include #include #include using namespace std; int main() { // Creating & Initializing a map of String & Ints map mapEx = { { 'aaa', 10 }, { 'ddd', 11 }, { 'bbb', 12 }, { 'ccc', 13 } }; // Create a map iterator and point to the end of map map::reverse_iterator it = mapEx.rbegin(); // Iterate over the map using Iterator till beginning. while (it != mapEx.rend()) { // Accessing KEY from element pointed by it. string word = it->first; // Accessing VALUE from element pointed by it. int count = it->second; cout << word << ' :: ' << count << endl; // Increment the Iterator to point to next entry it++; } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> ddd :: 11 ccc :: 13 bbb :: 12 aaa :: 10 </pre> <p>In the above example, we are using while loop to iterate over the map in reverse order.</p> <p>Because map store the elements in sorted order of keys therefore, iterating over a map will result in above order i.e. sorted order of keys.</p> <h2>Example 3</h2> <p>Let's see a simple example.</p> <pre> #include #include using namespace std; int main(void) { /* Initializer_list constructor */ map m = { {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5}, }; cout << 'Map contains following elements in reverse order:' << endl; for (auto it = m.rbegin(); it != m.rend(); ++it) cout <first << '=" <second << endl; return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Map contains following elements in reverse order: e = 5 d = 4 c = 3 b = 2 a = 1 </pre> <p>In the above example, elements of map returned in a reverse order.</p> <h2 >Example 4</h2> <p>Let" s see a simple example to sort and calculate the highest marks.< p> <pre> #include #include #include using namespace std; int main () { map emp = { { 1000, 10}, { 2500, 20 }, { 4500, 30 }, { 3000, 40 }, { 5500, 50 }}; cout << 'Salary' << ' | ' << 'ID' << ' '; cout<<'______________________ '; map::reverse_iterator rit; for (rit="emp.rbegin();" rit!="emp.rend();" ++rit) cout <first << ' | <second ' '; auto ite="emp.rbegin();" ' highest salary: '<first <<' '; 'id is: '<second return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Salary | ID ______________________ 5500 | 50 4500 | 30 3000 | 40 2500 | 20 1000 | 10 Highest salary: 5500 ID is: 50 </pre> <p>In the above example, a map emp is implemented where the ID is being stored as value and salary as key. This enables us to take advantage of the auto sorting in maps and lets us to identify the ID of the element with the highest salary.</p></'______________________ ';></pre></first></pre></first>
V zgornjem primeru se funkcija rend() uporablja za vrnitev povratnega iteratorja elementu, ki sledi zadnjemu elementu obrnjenega vsebnika.
Ker zemljevid hrani elemente v razvrščenem vrstnem redu ključev, bo ponavljanje po zemljevidu povzročilo zgornji vrstni red, tj. razvrščeni vrstni red ključev.
aja java
Primer 2
Oglejmo si preprost primer iteracije po zemljevidu v obratnem vrstnem redu z uporabo zanke while:
#include #include #include #include using namespace std; int main() { // Creating & Initializing a map of String & Ints map mapEx = { { 'aaa', 10 }, { 'ddd', 11 }, { 'bbb', 12 }, { 'ccc', 13 } }; // Create a map iterator and point to the end of map map::reverse_iterator it = mapEx.rbegin(); // Iterate over the map using Iterator till beginning. while (it != mapEx.rend()) { // Accessing KEY from element pointed by it. string word = it->first; // Accessing VALUE from element pointed by it. int count = it->second; cout << word << ' :: ' << count << endl; // Increment the Iterator to point to next entry it++; } return 0; }
Izhod:
np.edinstveno
ddd :: 11 ccc :: 13 bbb :: 12 aaa :: 10
V zgornjem primeru uporabljamo zanko while za ponavljanje zemljevida v obratnem vrstnem redu.
Ker zemljevid hrani elemente v razvrščenem vrstnem redu ključev, bo ponavljanje po zemljevidu povzročilo zgornji vrstni red, tj. razvrščeni vrstni red ključev.
Primer 3
Poglejmo preprost primer.
#include #include using namespace std; int main(void) { /* Initializer_list constructor */ map m = { {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5}, }; cout << 'Map contains following elements in reverse order:' << endl; for (auto it = m.rbegin(); it != m.rend(); ++it) cout <first << \'=" <second << endl; return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Map contains following elements in reverse order: e = 5 d = 4 c = 3 b = 2 a = 1 </pre> <p>In the above example, elements of map returned in a reverse order.</p> <h2 >Example 4</h2> <p>Let" s see a simple example to sort and calculate the highest marks.< p> <pre> #include #include #include using namespace std; int main () { map emp = { { 1000, 10}, { 2500, 20 }, { 4500, 30 }, { 3000, 40 }, { 5500, 50 }}; cout << 'Salary' << ' | ' << 'ID' << ' '; cout<<\'______________________ \'; map::reverse_iterator rit; for (rit="emp.rbegin();" rit!="emp.rend();" ++rit) cout <first << \' | <second \' \'; auto ite="emp.rbegin();" \' highest salary: \'<first <<\' \'; \'id is: \'<second return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Salary | ID ______________________ 5500 | 50 4500 | 30 3000 | 40 2500 | 20 1000 | 10 Highest salary: 5500 ID is: 50 </pre> <p>In the above example, a map emp is implemented where the ID is being stored as value and salary as key. This enables us to take advantage of the auto sorting in maps and lets us to identify the ID of the element with the highest salary.</p></\'______________________ \';></pre></first>
V zgornjem primeru je implementiran zemljevid emp, kjer je ID shranjen kot vrednost in plača kot ključ. To nam omogoča, da izkoristimo samodejno razvrščanje v zemljevidih in nam omogoča, da prepoznamo ID elementa z najvišjo plačo.
\'______________________ \';>