logo

Fonction C++ map rend()

Carte C++ rend() La fonction est utilisée pour renvoyer un itérateur à la fin de la carte (pas le dernier élément mais le dernier élément) dans ordre inverse . Ceci est similaire à l'élément précédant le premier élément du conteneur non inversé.

Remarque : - Ceci est un espace réservé. Aucun élément n'existe à cet emplacement et tenter d'y accéder est un comportement indéfini.

Syntaxe

 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ètre

Aucun

hauteur de décalage

Valeur de retour

Il renvoie un itérateur inverse à l'élément suivant le dernier élément du conteneur inversé.

Exemple 1

Voyons l'exemple simple de la fonction rend() :

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

Dans l'exemple ci-dessus, la fonction rend() est utilisée pour renvoyer un itérateur inverse à l'élément suivant le dernier élément du conteneur inversé.

Étant donné que map stocke les éléments dans l'ordre trié des clés, une itération sur une carte entraînera l'ordre ci-dessus, c'est-à-dire l'ordre trié des clés.

trouver dans la carte C++

Exemple 2

Voyons un exemple simple pour parcourir la carte dans l'ordre inverse en utilisant la boucle while :

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

Sortir:

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

Dans l'exemple ci-dessus, nous utilisons la boucle while pour parcourir la carte dans l'ordre inverse.

Étant donné que map stocke les éléments dans l'ordre trié des clés, une itération sur une carte entraînera l'ordre ci-dessus, c'est-à-dire l'ordre trié des clés.

Exemple 3

Voyons un exemple simple.

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

Dans l'exemple ci-dessus, une carte emp est implémentée où l'ID est stocké comme valeur et le salaire comme clé. Cela nous permet de profiter du tri automatique dans les cartes et nous permet d'identifier l'ID de l'élément avec le salaire le plus élevé.

java codage if else instruction