C++ kart gjengi() funksjonen brukes til å returnere en iterator til slutten av kartet (ikke det siste elementet, men det siste siste elementet) i omvendt rekkefølge . Dette ligner på elementet foran det første elementet i den ikke-reverserte beholderen.
Merk:- Dette er en plassholder. Det finnes ikke noe element på denne plasseringen, og forsøk på tilgang er udefinert atferd.
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
Returverdi
Den returnerer en omvendt iterator til elementet som følger det siste elementet i den reverserte beholderen.
Eksempel 1
La oss se det enkle eksemplet for rend()-funksjonen:
#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>
I eksemplet ovenfor brukes rend()-funksjonen til å returnere en omvendt iterator til elementet etter det siste elementet i den reverserte beholderen.
Fordi kart lagrer elementene i sortert rekkefølge av nøkler derfor, vil iterasjon over et kart resultere i over rekkefølge, dvs. sortert rekkefølge av nøkler.
Eksempel 2
La oss se et enkelt eksempel for å iterere over kartet i omvendt rekkefølge ved å bruke while loop:
#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; }
Produksjon:
ddd :: 11 ccc :: 13 bbb :: 12 aaa :: 10
I eksemplet ovenfor bruker vi while loop for å iterere over kartet i omvendt rekkefølge.
Fordi kart lagrer elementene i sortert rekkefølge av nøkler derfor, vil iterasjon over et kart resultere i over rekkefølge, dvs. sortert rekkefølge av nøkler.
Eksempel 3
La oss se et enkelt eksempel.
#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>
I eksemplet ovenfor er en kart-emp implementert der IDen lagres som verdi og lønn som nøkkel. Dette gjør oss i stand til å dra nytte av automatisk sortering i kart og lar oss identifisere ID-en til elementet med høyest lønn.
\'______________________ \';>