logo

C++ map rend() függvény

C++ térkép Vakol() A függvény egy iterátor visszaküldésére szolgál a térkép végére (nem az utolsó elemre, hanem az utolsó utolsó elemre). fordított sorrendben . Ez hasonló a nem fordított tároló első elemét megelőző elemhez.

Megjegyzés:- Ez egy helyőrző. Ezen a helyen nem található elem, és a hozzáférési kísérlet meghatározatlan viselkedés.

Szintaxis

 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 

Paraméter

Egyik sem

Visszatérési érték

Visszaküldi a fordított iterátort a fordított tároló utolsó elemét követő elemhez.

1. példa

Lássuk az egyszerű példát a rend() függvényre:

 #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>

A fenti példában a rend() függvény arra szolgál, hogy visszaadja a fordított iterátort a fordított tároló utolsó elemét követő elemhez.

Mivel a térkép az elemeket a kulcsok rendezett sorrendjében tárolja, ezért a térképen való iteráció a fenti sorrendet, azaz a kulcsok rendezett sorrendjét eredményezi.

2. példa

Lássunk egy egyszerű példát a térképen fordított sorrendben történő iterációra a while ciklus használatával:

 #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; } 

Kimenet:

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

A fenti példában a while ciklust használjuk a térképen fordított sorrendben történő iterációhoz.

Mivel a térkép az elemeket a kulcsok rendezett sorrendjében tárolja, ezért a térképen való iteráció a fenti sorrendet, azaz a kulcsok rendezett sorrendjét eredményezi.

3. példa

Lássunk egy egyszerű példát.

 #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>

A fenti példában egy map emp van megvalósítva, ahol az azonosító értékként van tárolva, és a fizetés kulcsként. Ez lehetővé teszi számunkra, hogy kihasználjuk az automatikus rendezés előnyeit a térképeken, és azonosítsuk a legmagasabb fizetésű elem azonosítóját.