logo

C++ 지도 렌드() 함수

C++ 지도 세우다() 함수는 맵의 끝(마지막 요소가 아니라 과거의 마지막 요소)으로 반복자를 반환하는 데 사용됩니다. 역순으로 . 이는 반전되지 않은 컨테이너의 첫 번째 요소 앞에 있는 요소와 유사합니다.

참고:- 이것은 자리 표시자입니다. 이 위치에는 요소가 없으며 액세스 시도는 정의되지 않은 동작입니다.

통사론

 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 

매개변수

없음

반환 값

역방향 컨테이너의 마지막 요소 다음 요소에 역방향 반복자를 반환합니다.

실시예 1

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>

위의 예에서 rend() 함수는 역방향 컨테이너의 마지막 요소 다음 요소에 역방향 반복자를 반환하는 데 사용됩니다.

따라서 맵은 정렬된 키 순서로 요소를 저장하므로 맵을 반복하면 위의 순서, 즉 키 정렬 순서가 발생합니다.

실시예 2

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

산출:

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

위의 예에서는 while 루프를 사용하여 지도를 역순으로 반복합니다.

따라서 맵은 정렬된 키 순서로 요소를 저장하므로 맵을 반복하면 위의 순서, 즉 키 정렬 순서가 발생합니다.

크롬 주소 표시줄

실시예 3

간단한 예를 살펴보겠습니다.

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

위의 예에서는 ID가 값으로 저장되고 급여가 키로 저장되는 맵 emp가 구현됩니다. 이를 통해 맵의 자동 정렬 기능을 활용하고 급여가 가장 높은 요소의 ID를 식별할 수 있습니다.