The insert() Method is used to insert a new Entry in a Map
Let us say, we have a Map that contains,
As Key and Value pair.
Now, let us say, we want to insert a new entry,
Rose, Is a Flower
Where Rose is the Key and Is a Flower is its corresponding Value.
Let us see in the below example.
#include <iostream> #include <map> using namespace std; int main() { map<string, string> x = { { "Five", "Is a Number" }, { "John", "Is a Name" }, { "C++", "Is a Language"} }; x.insert({"Rose", "Is a Flower"}); for (auto i : x) { cout << "The Key is : " << i.first << " and the Value is :" << i.second << endl; } return 0; }
So, in the above code we have created a Map using braces {} and Key and Valuepairs.
mapx = { { "Five", "Is a Number" }, { "John", "Is a Name" }, { "C++", "Is a Language"} };
And initialised to the variable x.
Now, we are supposed to add a new entry to the Map. Where the Key would beRose and the Value Is a Flower.
So, we have used the insert() method to add it.
x.insert({"Rose", "Is a Flower"});
And the entry is added to the Map.
And we get the below output,