Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - UPDATE MAP


How to update the entries of a Map?


Let us say, we have a Map that contains,

Five, Is a Number

John, Is a Name

C++, Is a Language

As Key and Value pair.


Now, let us say, we want to update the value of the Key, Five with Is an one digit number.

Five, Is an one digit number

Where Five is the Key and Is an one digit number is the new Value.


Let us see in the below example.


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["Five"] = "Is an one digit number";
        
    for (auto i : x)  
    {  
        cout << "The Key is : " << i.first << " and the Value is :"  << i.second << endl;  
    }
    
    return 0;    
}


Output :



  The Key is : C++ and the Value is :Is a Language
  The Key is : Five and the Value is :Is an one digit number
  The Key is : John and the Value is :Is a Name

So, in the above code we have created a Map using braces {} and Key and Value pairs.


map<string, string> x =
	{
		{ "Five", "Is a Number" },
		{ "John", "Is a Name" },
		{ "C++", "Is a Language"}
	};

And initialised to the variable x.

java_Collections

Now, we are supposed to update the value of the Key, Five with Is an one digit number.


So, we have used the below way to update it.


x["Five"] = "Is an one digit number";
java_Collections


And the entry for the Key Five is updated in the Map.

java_Collections

And we get the below output,

Output :



  The Key is : C++ and the Value is :Is a Language
  The Key is : Five and the Value is :Is an one digit number
  The Key is : John and the Value is :Is a Name