logo

C++ map rend() Funktion

C++ kort render() funktion bruges til at returnere en iterator til slutningen af ​​kortet (ikke det sidste element, men det sidste sidste element) i omvendt rækkefølge . Dette svarer til det element, der går forud for det første element i den ikke-omvendte beholder.

Bemærk: - Dette er en pladsholder. Der findes intet element på denne placering, og forsøg på at få adgang er udefineret adfærd.

Syntaks

 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

Ingen

Returværdi

Det returnerer en omvendt iterator til elementet efter det sidste element i den omvendte beholder.

Eksempel 1

Lad os se det enkle eksempel på rend()-funktionen:

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

I ovenstående eksempel bruges funktionen rend() til at returnere en omvendt iterator til elementet efter det sidste element i den omvendte beholder.

Fordi kortet gemmer elementerne i sorteret rækkefølge af nøgler, vil iteration over et kort derfor resultere i ovenstående rækkefølge, dvs. sorteret rækkefølge af nøgler.

Eksempel 2

Lad os se et simpelt eksempel på at iterere over kortet i omvendt rækkefølge ved hjælp af while-løkke:

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

Produktion:

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

I ovenstående eksempel bruger vi while-løkke til at iterere over kortet i omvendt rækkefølge.

Fordi kortet gemmer elementerne i sorteret rækkefølge af nøgler, vil iteration over et kort derfor resultere i ovenstående rækkefølge, dvs. sorteret rækkefølge af nøgler.

Eksempel 3

Lad os se et simpelt eksempel.

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

I ovenstående eksempel implementeres en map emp, hvor ID'et gemmes som værdi og løn som nøgle. Dette gør det muligt for os at drage fordel af den automatiske sortering i kort og lader os identificere ID'et på elementet med den højeste løn.