logo

C++ funkcija mapiranja rend().

C++ mapa render() funkcija se koristi za vraćanje iteratora na kraj mape (ne zadnji element, već zadnji zadnji element) u obrnuti redoslijed . Ovo je slično elementu koji prethodi prvom elementu neobrnutog spremnika.

Napomena:- Ovo je rezervirano mjesto. Na ovoj lokaciji ne postoji nijedan element i pokušaj pristupa je nedefinirano ponašanje.

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 

Parametar

Nijedan

Povratna vrijednost

Vraća obrnuti iterator elementu koji slijedi nakon posljednjeg elementa obrnutog spremnika.

Primjer 1

Pogledajmo jednostavan primjer za rend() funkciju:

 #include #include using namespace std; int main () { map mymap; mymap[&apos;x&apos;] = 100; mymap[&apos;y&apos;] = 200; mymap[&apos;z&apos;] = 300; // show content: map::reverse_iterator rit; for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit) cout <first << '=" &lt;second &lt;&lt; " 
'; 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&apos;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 &amp; Initializing a map of String &amp; Ints map mapEx = { { &apos;aaa&apos;, 10 }, { &apos;ddd&apos;, 11 }, { &apos;bbb&apos;, 12 }, { &apos;ccc&apos;, 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-&gt;first; // Accessing VALUE from element pointed by it. int count = it-&gt;second; cout &lt;&lt; word &lt;&lt; &apos; :: &apos; &lt;&lt; count &lt;&lt; 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&apos;s see a simple example.</p> <pre> #include #include using namespace std; int main(void) { /* Initializer_list constructor */ map m = { {&apos;a&apos;, 1}, {&apos;b&apos;, 2}, {&apos;c&apos;, 3}, {&apos;d&apos;, 4}, {&apos;e&apos;, 5}, }; cout &lt;&lt; &apos;Map contains following elements in reverse order:&apos; &lt;&lt; endl; for (auto it = m.rbegin(); it != m.rend(); ++it) cout <first << '=" &lt;second &lt;&lt; endl; return 0; } &lt;/pre&gt; &lt;p&gt; &lt;strong&gt;Output:&lt;/strong&gt; &lt;/p&gt; &lt;pre&gt; Map contains following elements in reverse order: e = 5 d = 4 c = 3 b = 2 a = 1 &lt;/pre&gt; &lt;p&gt;In the above example, elements of map returned in a reverse order.&lt;/p&gt; &lt;h2 &gt;Example 4&lt;/h2&gt; &lt;p&gt;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 &lt;&lt; &apos;Salary&apos; &lt;&lt; &apos; | &apos; &lt;&lt; &apos;ID&apos; &lt;&lt; &apos;
&apos;; cout&lt;<'______________________
'; 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>

U gornjem primjeru, funkcija rend() koristi se za vraćanje obrnutog iteratora elementu koji slijedi nakon posljednjeg elementa obrnutog spremnika.

kako ažurirati u Javi

Budući da mapa pohranjuje elemente u sortiranom redoslijedu ključeva, iteracija preko mape rezultirat će gornjim redoslijedom, tj. sortiranim redoslijedom ključeva.

Primjer 2

Pogledajmo jednostavan primjer za ponavljanje karte obrnutim redoslijedom pomoću while petlje:

 #include #include #include #include using namespace std; int main() { // Creating &amp; Initializing a map of String &amp; Ints map mapEx = { { &apos;aaa&apos;, 10 }, { &apos;ddd&apos;, 11 }, { &apos;bbb&apos;, 12 }, { &apos;ccc&apos;, 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-&gt;first; // Accessing VALUE from element pointed by it. int count = it-&gt;second; cout &lt;&lt; word &lt;&lt; &apos; :: &apos; &lt;&lt; count &lt;&lt; endl; // Increment the Iterator to point to next entry it++; } return 0; } 

Izlaz:

 ddd :: 11 ccc :: 13 bbb :: 12 aaa :: 10 

U gornjem primjeru koristimo while petlju za ponavljanje karte obrnutim redoslijedom.

Budući da mapa pohranjuje elemente u sortiranom redoslijedu ključeva, iteracija preko mape rezultirat će gornjim redoslijedom, tj. sortiranim redoslijedom ključeva.

Primjer 3

Pogledajmo jednostavan primjer.

napomene u proljetnom pokretanju
 #include #include using namespace std; int main(void) { /* Initializer_list constructor */ map m = { {&apos;a&apos;, 1}, {&apos;b&apos;, 2}, {&apos;c&apos;, 3}, {&apos;d&apos;, 4}, {&apos;e&apos;, 5}, }; cout &lt;&lt; &apos;Map contains following elements in reverse order:&apos; &lt;&lt; endl; for (auto it = m.rbegin(); it != m.rend(); ++it) cout <first << \'=" &lt;second &lt;&lt; endl; return 0; } &lt;/pre&gt; &lt;p&gt; &lt;strong&gt;Output:&lt;/strong&gt; &lt;/p&gt; &lt;pre&gt; Map contains following elements in reverse order: e = 5 d = 4 c = 3 b = 2 a = 1 &lt;/pre&gt; &lt;p&gt;In the above example, elements of map returned in a reverse order.&lt;/p&gt; &lt;h2 &gt;Example 4&lt;/h2&gt; &lt;p&gt;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 &lt;&lt; &apos;Salary&apos; &lt;&lt; &apos; | &apos; &lt;&lt; &apos;ID&apos; &lt;&lt; &apos;
&apos;; cout&lt;<\'______________________
\'; 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>

U gornjem primjeru, mapa emp implementirana je gdje se ID pohranjuje kao vrijednost, a plaća kao ključ. To nam omogućuje da iskoristimo prednosti automatskog sortiranja u kartama i dopušta nam da identificiramo ID elementa s najvećom plaćom.