The emplace function in C++ map is employed to expand the map container by adding fresh elements directly into the container without copying or moving them.
The constructor of the element is invoked with the arguments provided to this function. Insertion occurs only if the key is not already present.
Syntax
template <class...Args>
pair<iterator, bool> emplace (Args&&... args); //since C++ 11
Parameter
The arguments passed to instantiate an element for insertion into the map.
Return value
It provides a boolean pair indicating whether the insertion occurred and gives back an iterator that points to the newly added element.
Example 1
Let's explore a straightforward example of adding elements to a map.
#include <iostream>
#include <map>
using namespace std;
int main(void) {
map<char, int> m;
m.emplace('a', 1);
m.emplace('b', 2);
m.emplace('c', 3);
m.emplace('d', 4);
m.emplace('e', 5);
cout << "Map contains following elements" << endl;
for (auto it = m.begin(); it != m.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
Output:
Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5
In the preceding example, it merely adds the element to the map m using the provided key-value pairs.
Example 2
Let's consider a basic example to insert the element and verify the presence of a duplicate key.
#include <map>
#include <string>
#include <iostream>
using namespace std;
template <typename M> void print(const M& m) {
cout << m.size() << " elements: ";
for (const auto& p : m) {
cout << "(" << p.first << ", " << p.second << ") ";
}
cout << endl;
}
int main()
{
map<int, string> m1;
auto ret = m1.emplace(10, "ten");
ret = m1.emplace(20, "twenty");
ret= m1.emplace(30,"thirty");
if (!ret.second){
auto pr = *ret.first;
cout << "Emplace failed, element with key 10 already exists."
<< endl << " The existing element is (" << pr.first << ", " << pr.second << ")"
<< endl;
cout << "map not modified" << endl;
}
else{
cout << "map modified, now contains \n";
print(m1);
}
cout << endl;
ret = m1.emplace(10, "one zero");
if (!ret.second){
auto pr = *ret.first;
cout << "Emplace failed, element with key 10 already exists."
<< endl << " The existing element is (" << pr.first << ", " << pr.second << ")"
<< endl;
}
else{
cout << "map modified, now contains ";
print(m1);
}
cout << endl;
}
Output:
map modified, now contains
3 elements: (10, ten) (20, twenty) (30, thirty)
Emplace failed, element with key 10 already exists.
The existing element is (10, ten)
In the scenario described, elements are added to the map, and if an attempt is made to insert an element with the key value of 10, an error message will be shown indicating that the key 10 is already present in the map.
Example 3
Let's explore a basic illustration of inserting elements into a map by providing a constructor argument for both key and value correspondingly.
#include <iostream>
#include <utility>
#include <string>
using namespace std;
#include <map>
int main()
{
map<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("d", "ddd");
// 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
b => abcd
c => cccccccccc
d => ddd
In the example provided, elements are added to the map by supplying a constructor argument for both the key and the value individually.
Example 4
Let's see a simple example to insert the element.
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
typedef map<string, int> city;
string name;
int age;
city fmly ;
int n;
cout<<"Enter the number of fmly 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[name] = age; // Put them in map
fmly.emplace(name,age);
}
cout<<"\nTotal memnber of fmly is:"<< fmly.size();
cout<<"\nDetails of fmly 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 fmly members : 3
Enter the name and age of each member:
Ram 42
Sita 37
Laxman 40
Total memnber of fmly is:3
Details of fmly members:
Name | Age
__________________________
Laxman | 40
Ram | 42
Sita | 37
In the previous example, it seamlessly adds the elements based on the user's selection.