In C++, a multimap is an associative data structure that permits multiple elements to be associated with the same key. By default, a multimap organizes its keys in ascending order, but this can be adjusted based on specific needs.
In the realm of C++, the built-in standard function known as multimap crbegin is a fundamental feature within the Standard Template Library (STL). Essentially, the purpose of the multimap crbegin function is to provide a constant reverse iterator that points to the final element in the multimap container. This iterator is immutable, similar to rbegin, preventing any alterations to the elements it references. As a constant reverse iterator for a multimap, it traverses in the opposite direction, advancing towards the initial (first) element of the multimap container while indicating a constant element.
Syntax
It has the following syntax:
const_reverse_iterator crbegin() const noexcept;//since C++ 11
multimapName.crbegin;
In this particular syntax, the crbegin function of the multimap does not accept any arguments.
Return value
It yields a constant reverse iterator that points to the final element of the multimap based on the arrangement of data within the multimap.
Time Complexity
The time complexity of the multimap crbegin method remains constant, specifically O(1).
Iterator validity
The iterator employed in the multimap crbegin method maintains its validity without any alterations.
Now, let's explore some instances to delve further into the multimap crbegin scenario.
C++ Multimap crbegin Example
Let's consider a C++ scenario to demonstrate the crbegin function for a multimap, where the keys are characters and the values are strings.
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 illustration, a multimap was established with character keys and textual values, and specific key-value combinations were added. Subsequently, a for loop was employed in conjunction with crbegin and crend to iterate through the multimap in reverse sequence. Since the multimap organizes elements based on key sorting, the reverse traversal initiates from the greatest key, displaying elements associated with key 'b' (Python, C++) initially, and then showcasing elements linked to key 'a' (Java, Android).
Traversing a Multimap in Reverse Order using crbegin and crend
Let's consider a scenario where we iterate through the multimap backwards by employing 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 instance, we are examining a multimap containing string keys and integer values. It has been initialized with specific key-value pairs and subsequently iterated in reverse using a constreverseiterator alongside the crbegin and crend functions. Following this, a while loop is employed to retrieve both the key and its associated value, with the iterator advancement moving to the subsequent entry during each iteration. Given that a multimap inherently maintains elements in the sorted order of keys, the reverse traversal displays the output starting from the key "ddd" and descending to "aaa".
Accessing the Last Element of a Multimap using crbegin
To demonstrate retrieving the initial element of the reversed multimap in C++, we will utilize the crbegin function.
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 instance, a multimap has been created with integer keys and integer values. Subsequently, data has been added to this multimap, followed by the invocation of the crbegin method on the multimap m1. The outcome has been displayed using the cout statement. Finally, the function concludes by returning 0. Since the multimap arranges elements in ascending order, crbegin references the element with the highest key (2, 30).
Reverse Traversal of a Multimap and Accessing the Top Element using crbegin
Let's consider a basic example to arrange and determine the top score using the crbegin method 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 instance, a multimap was instantiated with integer keys and values, and it was populated with data. Subsequently, a constreverseiterator was employed using crbegin and crend to iterate through the multimap in reverse, displaying all entries. Then, crbegin was utilized once more to retrieve the top scorer's roll number and their highest marks. The multimap's sorted key storage facilitates straightforward identification of the maximum key through reverse traversal.
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 summary, the C++ multimap::crbegin method offers a constant reverse iterator pointing to the last element in a multimap. This iterator facilitates a read-only traversal in reverse, starting from the largest key to the smallest. This functionality proves valuable for scenarios where accessing elements in reverse order without altering them is necessary. By leveraging the automatic key-based sorting inherent in multimaps, it simplifies the identification of specific elements, like the highest key or value. Therefore, crbegin serves as a valuable tool for conducting reverse read-only operations on multimaps.
C++ multimap crbegin Method FAQs
The primary objective of the multimap::crbegin function in C++ is to return a constant reverse iterator pointing to the last element in a multimap container.
In C++, the crbegin method in multimap is employed to provide a constant reverse iterator pointing to the final element within the multimap, facilitating backward traversal in a restricted, non-modifiable manner.
Using the crbegin function in C++ is preferred over utilizing the rbegin function because crbegin returns a constant reverse iterator pointing to the last element in a container, which prevents accidental modification of the elements.
In C++, the crbegin method proves to be valuable for conducting backward iterations without altering the elements, thereby guaranteeing the security in non-modifying tasks.
Yes, it is possible to make modifications to elements using the crbegin function in C++.
No, as the crbegin method provides only read access to the elements within C++.
4) Does the crbegin function exist in all versions of the C++ standard?
Yes, the crbegin method was added in C++11 and is available in all subsequent releases.
5) What is the time complexity of the crbegin method in C++?
The crbegin method exhibits a time complexity of O(1), indicating constant time efficiency.