In the C++ coding language, the multimap represents an associative data structure available within the Standard Template Library (STL). Its primary function is to organize key-value pairs in a sorted order, allowing for duplicate keys. Multimap offers a variety of methods for adding, modifying, accessing, iterating, and searching for values, such as insert, find, begin, end, and various additional functions.
In C++, the ```
Room1 = 100
Room1 = 300
Room1 = 100
Room2 = 200
### Syntax
It has the following syntax:
const_iterator cend const noexcept; //since C++ 11
< multimapName >.cend
In this structure, a const_iterator represents an iterator that points to constant data in C++ tutorials.
Parameter: The cend( ) function of multimap does not accept any arguments.
It provides a constant iterator that references the final element of the multimap based on the data arrangement within the multimap.
Time Complexity: The time complexity of the multimap's cend() method is constant, meaning it operates in O(1) time.
Iterator integrity: The iterator employed in the multimap cend() method remains unchanged in its validity.
### C++ Simple Multimap_cend() Function Example
Let's consider an instance to demonstrate the multimap cend() method with keys as characters and values as 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"}
};
// print content:
cout << "mymultimap contains:";
for (auto it = mymultimap.cbegin; it != mymultimap.cend; ++it)
cout << " [" << (it).first << ':' << (it).second << ']';
cout << '\n';
return 0;
}
Output:
mymultimap contains: [a:Java] [a:Android] [b:C++] [b:Python]
Explanation:
In this illustration, a multimap was established featuring char keys paired with string values. Subsequently, Key-value pairs were added into the multimap. Through a for loop, the iteration is carried out from the cbegin() to cend() methods. The cend() method provides a constant iterator positioned beyond the final element, guaranteeing restricted read-only access. Ultimately, the elements are displayed in ascending key order: initially, all elements with the key 'a' ("Java", "Android") are presented, succeeded by elements with the key 'b' ("C++", "Python").
### C++ Multimap Example Using cbegin() and cend() with Lambda Function
Let's consider a basic C++ illustration to loop through the multimap using a range-based for loop, with string keys and integer values.
### Example
include <iostream>
include <map>
include <string>
include <iterator>
include <algorithm>
using namespace std; //using standard namespace
int main { //main function
multimap<string, int> m;
m= {
{"Room1", 100},
{"Room2", 200},
{"Room1", 300},
{"Room1", 100}
};
// Create a multimap iterator and point to the beginning of the multimap
multimap<string, int>::iterator it = m.begin;
// Iterate over a multimap using std::for_each and Lambda function
for_each(m.cbegin, m.cend,
(pair<string, int> element){
// Accessing KEY from element
string word = element.first;
// Accessing VALUE from element.
int count = element.second;
cout<<word<<" = "<<count<<endl;
});
return 0;
}
Output:
Room1 = 100
Room1 = 300
Room1 = 100
Room2 = 200
Explanation:
In this instance, we've established a multimap containing string keys and integer values. Subsequently, a multimap iterator is positioned at the initial point, and the std::for_each function, coupled with a lambda function, is employed to traverse through all elements. Within the lambda function, element.first fetches the key while element.second fetches the corresponding value. Ultimately, the elements are displayed in ascending key sequence, with multiple values for identical keys displayed based on their insertion order.
### C++ Multimap Example Using cend() and cbegin() with While Loop
Let's consider a basic C++ illustration to traverse through the multimap using a while loop, with integer keys and string elements.
### Example
include <iostream>
include <map>
include <string>
int main //main function
{
using namespace std; //using standard namespace
multimap<int,string> mymultimap = {
{ 100, "Jhonson"},
{ 100, "Danny" },
{ 200, "Michael" },
{ 300, "Jhony" },
{ 200, "Peter" }};
multimap<int, string>::const_iterator it; // declare an iterator
it = mymultimap.cbegin; // assign it to the start of the vector
while (it != mymultimap.cend) // while it hasn't reach the end
{
cout << it->first << " = " << it->second << "\n";
// print the value of the element icpp tutorials to
++it; // and iterate to the next element
}
cout << endl;
}
Output:
100 = Jhonson
100 = Danny
200 = Michael
200 = Peter
300 = Jhony
Explanation:
In this instance, we've established a multimap containing integer keys and string values. Following this, a constant iterator has been defined and initialized to the start position using the cbegin() function. A loop continues until reaching the end position with the cend() function (one beyond the last element), showcasing each key-value association. Ultimately, the elements are presented in increasing key sequence, where duplicate keys maintain their original insertion order.
### C++ Multimap Example Using the cbegin() and cend() functions with Element Access and Addition
Let's consider a straightforward example to demonstrate the cend() function in C++. Here, the keys and values are both of integer type, and the objective is to add each key to its corresponding value.
### Example
include <iostream>
include <string>
include <map>
using namespace std; //using standard namespace
int main //main function
{
multimap<int,int> mymultimap = {
{ 10, 10},
{ 20, 20 },
{ 10, 30 } };
cout<<"Elements are:" <<endl;
for (auto it = mymultimap.cbegin; it != mymultimap.cend; ++it)
cout << it->first
<< " + "
<< it->second
<< " = "
<<it->first + it->second
<< '\n';
auto ite = mymultimap.cend;
cout << "end element (point next to the last): ";
cout << "{" << ite->first << ", "
<< ite->second << "}\n";
return 0;
}
Output:
Elements are:
10 + 10 = 20
10 + 30 = 40
20 + 20 = 40
end element (point next to the last): {3, 0}
Explanation:
In this instance, we've established a multimap containing integer keys and integer values. Subsequently, a for loop is employed to traverse from cbegin() to cend(), showcasing both the key and its corresponding value, along with their total. Ultimately, the elements are exhibited in increasing key sequence, executing addition for each key-value association, and ultimately pointing out the position just beyond the final element.
## Features of Multimap cend() function in C++
There are several features of the multimap cend() function in C++. Some of them are as follows:
- In C++, it provides a const_iterator that provides read-only access to the multimap elements.
- It is usually connected with the cbegin() function to traverse the multimap without altering elements in C++.
- This function can be invoked on a const multimap, which helps to make it a better choice for immutable operations.
- In C++, the iteration using the cbegin() and cend functions follows the ascending order of keys by default.
- The cend() function of multimap returns a constant iterator pointing past the last element of the multimap.
## Conclusion
In summary, the cend() method is frequently employed to provide a constant iterator pointing to the position immediately following the final element within a container, like a multimap. This function is commonly paired with cbegin() to facilitate traversing elements in a read-only manner, ensuring no modifications are made. While cend() effectively signifies the end of the container and aids in managing iterations, it should never be dereferenced as it does not point to a valid element.
## C++ multimap cend() Function FAQs
The difference between end() and cend() in std::multimap in C++ is that end() returns an iterator to the element following the last element of the multimap, allowing for modification of the elements, while cend() returns a constant iterator pointing to the element following the last element, preventing modification of the elements in the multimap.
In C++, the end() method provides a modifiable iterator that enables altering container elements while iterating, like utilizing erase() or updating values through dereferencing. In contrast, the cend() function furnishes an immutable iterator that restricts modifications, making it suitable for exclusive read access.
No, the use of cend() does not impact the performance of iteration.
No, invoking the cend() method is an operation with constant time complexity and does not impact the performance of any iteration.
Yes, it is possible to utilize the cend() function to make modifications to the multimap in C++.
No, utilizing the cend() function to alter the multimap is not feasible. This function provides a const_iterator, preventing any modifications to both the elements and the container.
Common pitfalls or mistakes that can occur when utilizing the cend() function in C++ include:
- Incorrectly assuming cend() returns an iterator to the last element, when it actually points to the position after the last element in a container.
- Attempting to modify elements using the iterator returned by cend(), which is a constant iterator and cannot be used for modification.
- Forgetting that cend() is only available for containers that support const iterators, leading to compilation errors when used with incompatible containers.
- Misinterpreting the purpose of cend() and using it inappropriately in scenarios where the intention is to access the last element directly.
There are several pitfalls of using the cend() function in C++. Some of them are as follows:
- This function attempts to dereference or cause undefined behavior.
- It does not handle the case of an empty multimap.
- In C++, the iterator returned by the cend() function is a constant iterator, which means we cannot modify the elements icpp tutorials to.
- If we use the cend() function in iteration, it can cause an error.
5) Does the std::multimap::cend() function in C++ support thread safety?
No, the cend() method is inherently thread-safe as it is a const operation that does not alter the container. However, the iterators produced by the cend() method are not inherently thread-safe.