How to insert data in the map of strings? - GeeksforGeeks

14 Dec, 2020
Maps are associative containers that store elements in a specific order. It stores elements in a combination of key values and mapped values.
Syntax:
map<data type of key, data type of value> M
To use the above syntax for the map in C++, it is important to include the below header file:Header File:
#include <map>
To insert the data in the map insert() function in the map is used. It is used to insert elements with a particular key in the map container.
Syntax:
iterator map_name.insert({key, element})
Parameters: It accepts a pair that consists of a key and element which is to be inserted into the map container but it only inserts the unique key. This means that the function does not insert the key and element in the map if the key already exists in the map.
Return Value: It returns an iterator pointing to the new element in the map.
Below is the program to illustrate the same:
C++
// C++ program to store the string as// the map value#include <iostream>#include <map>using namespace std;  // Driver codeint main(){    // Get the Strings    string s = "abc";    string s1 = "bca";    string s2 = "cba";      // Declare map with both value    // and key  having string data_type    map<string, string> m;      // Insert the string in the map    m.insert(pair<string, string>(s1, s));    m.insert(pair<string, string>(s, s2));      // Print the elements stored    // in the map    for (auto itr = m.begin();         itr != m.end(); ++itr) {        cout << itr->first << '\t'             << itr->second << '\n';    }      return 0;}
abc    cba
bca    abc

There is another way to store the data in the map, below is the syntax for the same:
Syntax:
iterator map_name.insert(iterator position, {key, element})
Parameters: The function accepts two parameters which are described below:  
{key, element}: This specifies a pair that consists of a key and element which is to be inserted into the map container.position: It only points to a position from where the searching operation for insertion is to be started to make the process faster. The insertion is done according to the order which is followed by the container.
{key, element}: This specifies a pair that consists of a key and element which is to be inserted into the map container.
position: It only points to a position from where the searching operation for insertion is to be started to make the process faster. The insertion is done according to the order which is followed by the container.
Return Value: The function returns an iterator pointing to the new element in the container.
Below is the program to illustrate the same:
C++
// C++ program to illustrate the map// insert(iteratorposition, {key, element})#include <iostream>#include <map>using namespace std;  // Driver Codeint main(){    // Initialize a Map mp    map<string, int> mp;      // Insert elements in random order    mp.insert({ "abc", 30 });    mp.insert({ "bcd", 40 });      auto it = mp.find("bcd");      // Insert {"dcd", 60} starting the    // search from position where 2    // is present    mp.insert(it, { "dcd", 60 });      // Print the element    cout << "KEY\tELEMENT\n";      for (auto itr = mp.begin();         itr != mp.end(); ++itr) {        cout << itr->first << '\t'             << itr->second << '\n';    }      return 0;}
KEY    ELEMENT
abc    30
bcd    40
dcd    60

cpp-map
STL
C++
C++ Programs
Strings
Strings
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Header files in C/C++ and its uses
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
Sorting a Map by value in C++ STL