C++ multimap rbegin() functionThe C++ multimap rbegin() function is used to return a reverse iterator referring to the last element of the multimap container. A reverse iterator of multimap moves in reverse direction and incrementing it until it reaches to the beginning (First element) of the multimap container. SyntaxParameterNone Return valueIt returns a reverse iterator pointing to the last element of the multimap. ComplexityConstant. Iterator validityNo changes. Data racesThe container is accessed neither the const nor the non-const versions modify the container. Exception safetyThis function never throws exception. Example 1Let's see the simple example for rbegin() function: Output: c = 300 b = 200 a = 300 a = 100 In the above example, rbegin() function is used to return a reverse iterator pointing to the last element in the mymultimap multimap. Because multimap stores the elements in sorted order of keys therefore, iterating over a multimap will result in above order i.e. sorted order of keys. Example 2Let's see a simple example to iterate over the multimap in reverse order using while loop: Output: ddd :: 11 ccc :: 13 ccc :: 12 aaa :: 10 In the above example, we are using while loop to iterate over the multimap in reverse order and rbegin() function initializing the last element of the multimap. Because multimap stores the elements in sorted order of keys therefore, iterating over a multimap will result in above order i.e. sorted order of keys. Example 3Let's see a simple example to get the first element of the reversed multimap: Output: The first element of the reversed multimap m1 is: {4, 50} In the above example, rbegin() function returns the first element of the reversed multimap m1 i.e. {4,50}. Example 4Let's see a simple example to sort and calculate the highest marks: Output: Marks | Roll Number ______________________ 480 | 30 425 | 50 425 | 10 300 | 40 300 | 20 Highest Marks is: 480 Roll Number of Topper is: 30 In the above example, a multimap marks is implemented where the Roll Number is being stored as value and marks as key. This enables us to take advantage of the auto sorting in multimaps and lets us to identify the Roll number of the element with the highest marks. Next TopicC++ Multimap |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India