In the C++ programming language, a multimap is an associative container that allows multiple elements with the same key. A multimap by default follows the ascending order of keys to store the values into it, which can be changed later on as per the requirements.
In C++, the multimap crbegin function is a standard built-in function that is available in the Standard Template Library (STL). Basically, the multimap crbegin function is used to return a constant reverse iterator, which refers to the last element in the multimap container. It is the constant version of rbegin, which means we cannot modify the elements through the iterator it returns. A constant reverse iterator of a multimap moves in the reverse direction and increments it until it reaches the beginning (First element) of the multimap container and points to the constant element.
Syntax
It has the following syntax:
const_reverse_iterator crbegin() const noexcept;//since C++ 11
multimapName.crbegin;
In this syntax,
- The multimap crbegin function does not have any parameters.
Return value
It returns a constant reverse iterator pointing to the last element of the multimap as per the ordering of the data in the multimap.
Time Complexity
The time complexity of the multimap crbegin function is constant, i.e., O(1).
Iterator validity
The iterator used in the multimap crbegin function has no changes in its validity.
Now, let us look at a few examples to deep dive into the multimap crbegin context.
C++ Multimap crbegin Example
Let us take a C++ example to illustrate the multimap crbegin function, where keys are of char type, and elements are string values.
Example
#include <iostream>
#include <map>
using namespace std; //using standard namespace
int main () //main function
{
multimap<char,string> mymultimap;
mymultimap = {
{'a',"Java"},
{'b', "C++"},
{'b', "Python"},
{'a', "Android"}
};
cout << "mymultimap in reverse order:";
for (auto rit = mymultimap.crbegin(); rit != mymultimap.crend(); ++rit)
cout << " [" << rit->first << ':' << rit->second << ']';
cout << '\n';
return 0;
}
Output:
mymultimap in reverse order: [b:Python] [b:C++] [a:Android] [a:Java]
Explanation:
In this example, we have created a multimap with char keys and string values and inserted some key-value pairs. After that, we used a for loop with crbegin and crend that traversed the multimap in reverse order. As the multimap stores elements in sorted key order, the reverse iteration begins from the highest key, which prints elements with key 'b' (Python, C++) first, followed by elements with key 'a' (Java, Android).
Traversing a Multimap in Reverse Order using crbegin and crend
Let us take an example to iterate over the multimap in reverse order using a while loop in C++.
Example
#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std; //using standard namespace
int main() { //main function
// Creating & Initializing a multimap of String & Ints
multimap<string, int> multimapEx = {
{ "aaa", 10 },
{ "ddd", 11 },
{ "aaa", 12 },
{ "ccc", 13 }
};
// Create a multimap iterator and point to the end of the multimap
multimap<string, int>::const_reverse_iterator it = multimapEx.crbegin();
// Iterate over the multimap using Iterator till the beginning.
while (it != multimapEx.crend()) {
// Accessing KEY from the elemencpp tutorialed by it.
string word = it->first;
// Accessing VALUE from the elemencpp tutorialed by it.
int count = it->second;
cout << word << " :: " << count << endl;
// Increment the Iterator to point to the next entry
it++;
}
return 0;
}
Output:
ddd :: 11
ccc :: 13
aaa :: 12
aaa :: 10
Explanation:
In this example, we have taken a multimap with string keys and integer values that is initialized with some key-value pairs, and then traversed it in reverse order using a constreverseiterator with crbegin and crend functions. After that, we use a while loop that can access both the key and its corresponding value, which increments the iterator each time to move to the next entry. As a multimap always stores elements in sorted order of keys, the reverse iteration prints the output starting from key "ddd" down to "aaa".
Accessing the Last Element of a Multimap using crbegin
Let us take a simple example to get the first element of the reversed multimap using the crbegin function in C++.
Example
#include <iostream>
#include <string>
#include <map>
using namespace std; //using standard namespace
int main () //main function
{
multimap<int,int> m1 = {
{ 1, 10},
{ 2, 20 },
{ 2, 30 } };
auto ite = m1.crbegin();
cout << "The first element of the reversed multimap m1 is: ";
cout << "{" << ite->first << ", "
<< ite->second << "}\n";
return 0;
}
Output:
The first element of the reversed multimap m1 is: {2, 30}
Explanation:
In this example, we have initialized a multimap where the keys are of int type and the values are also of int type. After that, we have initialized data to the multimap, and then we have called the crbegin function on the multimap m1. Using the cout statement, we have printed the result. Finally, we have returned 0. As the multimap stores elements in sorted order, crbegin points to the element with the highest key (2, 30).
Reverse Traversal of a Multimap and Accessing the Top Element using crbegin
Let us take a simple example to sort and calculate the highest marks using the crbegin function in C++.
Example
#include <iostream>
#include <string>
#include <map>
using namespace std; //using standard namespace
int main () //main function
{
multimap<int,int> marks = {
{ 400, 10},
{ 300, 20 },
{ 400, 30 },
{ 300, 40 },
{ 480, 50 }};
cout << "Marks" << " | " << "Roll Number" << '\n';
cout<<"______________________\n";
multimap<int,int>::const_reverse_iterator rit;
for (rit=marks.crbegin(); rit!=marks.crend(); ++rit)
cout << rit->first << " | " << rit->second << '\n';
auto ite = marks.crbegin();
cout << "\nHighest Marks is: "<< ite->first <<" \n";
cout << "Roll Number of Topper is: "<< ite->second << "\n";
return 0;
}
Output:
Marks | Roll Number
_____________________
480 | 50
400 | 30
400 | 10
300 | 40
300 | 20
Highest Marks is: 480
Roll Number of Topper is: 50
Explanation:
In this example, we have created a multimap with integer keys and values and initialized it with data. After that, we use a constreverseiterator with crbegin and crend that traversed the multimap in reverse order to print all elements. Next, we have used the crbegin function again to access the highest marks and the corresponding roll number of the topper. As the multimap stores keys in sorted order, reverse iteration allows easy identification of the highest key.
Features of multimap crbegin function in C++
There are several features of the multimap crbegin function in C++. Some of them are as follows:
- In C++, elements cannot be modified using the crbegin function because it returns a constant iterator.
- It is very helpful when we want to have read-only access while iterating in reverse.
- It is commonly utilized to traverse the multimap in reverse order in C++.
- It returns a constreverseiterator thacpp tutorials to the last element of the multimap.
- In C++, multimaps store the elements in the sorted order of keys. Therefore, the reverse iteration naturally starts from the highest key.
Conclusion
In conclusion, the C++ multimap::crbegin function provides a constant reverse iterator to the last element of a multimap, which allows read-only reverse traversal from the largest key to the smallest. It is very helpful when we need to access elements in reverse order without modifying them. It takes advantage of the multimap's automatic key-based sorting and easily identifies elements, such as the highest key or value. The crbegin is very useful for reverse read-only operations on multimaps.
C++ multimap crbegin Method FAQs
1) What is the main purpose of the multimap::crbegin function in C++?
In the C++ programming language, the multimap crbegin function is used to return a constant reverse iterator thacpp tutorials to the last element of the multimap, which enables the reverse traversal in a read-only manner.
2) Why do we use the crbegin function instead of the rbegin function in C++?
In C++, the crbegin function is very useful when we need reverse traversal without modifying the elements, which ensures the safety in read-only operations.
3) Can we modify elements using the crbegin function in C++?
No, because the crbegin function offers read-only access to the elements in C++.
4) Is the crbegin function available in all C++ standards?
Yes, the crbegin function was introduced in C++11 and is present in all later versions.
5) What is the time complexity of the crbegin function in C++?
The crbegin function has the constant time complexity (O(1)).