In the C++ programming language, the multimap is an associative container that is provided by the Standard Template Library (STL). It is commonly utilized to store the key-value pairs in a sorted manner, which allows multiple elements to have the same key.
In C++ , the multimap emplace function is commonly utilized to extend the multimap container by inserting new elements into the container. Elements are built directly (neither copied nor moved). The constructor of the element is called by giving the arguments args passed to this function.
Syntax:
It has the following syntax:
template <class... Args>
iterator emplace (Args&&... args); //since C++ 11
In this syntax,
- args: It is used to represent the arguments forwarded to construct an element to be inserted into the multimap.
Return value:
The C++ emplace function indicates if the insertion has occurred or not and returns an iterator pointing to the newly inserted element.
C++ Simple multimap emplace function Example
Let us take a simple example to insert the elements into the multimap using the emplace function in C++.
Example
#include <iostream>
#include <map>
using namespace std;
int main(void) {
multimap<char, int> m;
m.emplace('a', 1);
m.emplace('b', 2);
m.emplace('c', 3);
m.emplace('b', 4);
m.emplace('c', 5);
cout << "Multimap contains the following elements" << endl;
for (auto it = m.begin(); it != m.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
Output:
Multimap contains the following elements
a = 1
b = 2
b = 4
c = 3
c = 5
Explanation:
In this example, we demonstrate the multimap::emplace function to construct and insert key-value pairs directly into a multimap without creating temporary objects. It enables multiple elements with the same key, so the duplicates are also inserted in the multimap. Here, we emplace multiple pairs in the multimap, and the output shows all elements stored in sorted order of keys, including duplicates.
C++ multimap::emplace Example with Duplicate Keys
Let us take a simple example to insert the element and check whether a duplicate key is allowed in a multimap or not in C++.
Example
#include <map>
#include <string>
#include <iostream>
using namespace std; //using standard namespace
template <typename M> void print(const M& m) {
cout << m.size() << " elements: " << endl;
for (const auto& p : m) {
cout << "(" << p.first << "," << p.second << ") ";
}
cout << endl;
}
int main() //main function
{
multimap<string, string> m1;
m1.emplace("Nikita", " Accounting");
m1.emplace("Amita", " Accounting");
m1.emplace("Deep", " Engineering");
cout << "Multimap modified, now contains ";
print(m1);
cout << endl;
m1.emplace("Nikita ", " Engineering");
cout << "Multimap modified, now contains ";
print(m1);
cout << endl;
}
Output:
Multimap modified, now contains 3 elements:
(Amita, Accounting) (Deep, Engineering) (Nikita, Accounting)
Multimap modified, now contains 4 elements:
(Amita, Accounting) (Deep, Engineering) (Nikita, Accounting) (Nikita, Engineering)
Explanation:
In this example, we have taken a multimap::emplace function that is used to insert key-value pairs directly into the container. Since a multimap enables duplicate keys, multiple values can be connected with the same key, such as "Nikita" being connected to both "Accounting" and "Engineering". After that, the elements are stored in sorted order of keys, and the program shows all entries after each insertion.
C++ Example to Show Different Ways to Use the multimap::emplace in C++
Let us take a simple example to insert the elements into the multimap by passing a constructor argument to a key and a value in C++.
Example
#include <iostream>
#include <utility>
#include <string>
#include <map>
using namespace std; //using standard namespace
int main() //main function
{
multimap<string, string> m;
// uses pair's move constructor
m.emplace(make_pair(string("a"), string("a")));
// uses pair's converting move constructor
m.emplace(make_pair("b", "abcd"));
// uses pair's template constructor
m.emplace("a", "aaa");
// uses pair's piecewise constructor
m.emplace(piecewise_construct,
forward_as_tuple("c"),
forward_as_tuple(10, 'c'));
for (const auto &p : m) {
cout << p.first << " => " << p.second << '\n';
}
}
Output:
a => a
a => aaa
b => abcd
c => cccccccccc
Explanation:
In this example, we demonstrate the multimap::emplace function by inserting elements using different constructor forms of std::pair. It shows insertion through a normal pair, converting constructors, direct key-value arguments, and the piecewise_construct technique for complex object creation. After that, the output prints all inserted elements, which shows how the emplace function effectively constructs entries in place.
C++ multimap::emplace Example for Storing Family Member Details
Let us take an example to illustrate how we can store the family member details using the multimap::emplace function in C++.
Example
#include <iostream>
#include <map>
#include <string>
using namespace std; //using standard namespace
int main() { //main function
typedef multimap<string, int> city;
string name;
int age;
city fmly ;
int n;
cout<<"Enter the number of family members:";
cin>>n;
cout<<"Enter the name and age of each member: \n";
for(int i =0; i<n; i++)
{
cin>> name; // Get key
cin>> age; // Get value
fmly.emplace(name,age); // Put them in multimap
}
cout<<"\nTotal members of the family is: "<< fmly.size();
cout<<"\nDetails of the family members: \n";
cout<<"\nName | Age \n ________________________\n";
city::iterator p;
for(p = fmly.begin(); p!=fmly.end(); p++)
{
cout<<(*p).first << " | " <<(*p).second <<" \n ";
}
return 0;
}
Output:
Enter the number of family members: 3
Enter the name and age of each member:
Jhonson 42
Maddy 37
Michael 40
Total members of the family is: 3
Details of the family members:
Name | Age
__________________________
Jhonson | 40
Maddy | 42
Michael | 37
Explanation:
In this example, we use a multimap::emplace function to store family member details, where the name is the key and the age is the value. Since a multimap allows duplicate keys, multiple members with the same name can be stored. After that, it prompts us to enter a number for each member, inserts the data into the multimap, and then shows the total number of members along with their details in sorted order by name.
Features of Multimap emplace function in C++
There are several features of the multimap emplace function in C++. Some of them are as follows:
- In the C++ programming language, the elements are created in-place inside the container, which helps us to prevent unnecessary operations.
- It takes only (O(log n) time complexity because insertions need to maintain the elements' order.
- It provides exception surety, which means if an exception is thrown, it doesn't affect the container.
- It enables duplicate keys by which the emplace function will insert the elements even if the same key is already present in the multimap.
Conclusion
In conclusion, the C++ multimap::emplace function provides an effective manner to construct and insert elements directly into a multimap without creating temporary objects. A multimap enables duplicate keys, by which multiple values can be connected with the same key.
Since the emplace function constructs elements in place, it provides better performance than the insert function in many scenarios. Overall, the emplace function is an essential and flexible method to insert elements into a multimap while handling sorted order and supporting duplicate entries.
C++ Multimap::emplace function FAQs
1) What is the main use of the multimap::emplace function in C++?
In the C++ programming language, the emplace function is commonly utilized to create and insert key-value pairs directly into a multimap. It helps to prevent creating temporary objects, which makes insertion more efficient compared to the insert function.
2) What is the time complexity of the multimap::emplace function in C++?
In C++, the time complexity of the multimap::emplace function is O(log n) because insertion needs to maintain the sorted order of elements inside the multimap.
3) Can we use emplace with an iterator hint in a multimap in C++?
Yes. There is an overloaded version that accepts a position hint (iterator) in the C++ programming language, which can help to enhance the insertion efficiency if the hint is close to the correct position.
4) What is the main difference between the emplace and insert functions in C++?
In the C++ programming language, the main difference between the emplace and insert functions is that the emplace function is used to create the elements directly inside the container, which is very effective in programming. On the other hand, the insert function is used to create a temporary object before inserting.
5) Can the emplace function be utilized with complex objects as a value in C++?
Yes, we can use the emplace function with complex objects as a value in C++. It is a better choice to insert complex objects without creating extra temporary copies.
6) How does the emplace function maintain the element orders in a multimap in C++?
In C++, mutimap is commonly utilized to store the elements in a sorted order of keys. The emplace function helps to ensure that the sorted order is maintained during the insertion.