C++ Program to Update value of Dictionary using key


Many computer languages provide dictionaries, which are a type of data structure. A dictionary is one type of faster data structure that stores data based on keys and values. It retains the key−value combination there so that the keys can easily search for certain components in almost real time. The C++ STL language standard includes a dictionaries−like data structure. The term "map" is used to describe this data structure. The map creates a pair of keys and values of any type (since we are using C++, the type must be defined before compilation). This section will demonstrate how to update values in a map or dictionary that already exists, in C++.

Let's first examine the definition of the map data structure. These interior templates need two different sorts. The following displays the syntax and necessary libraries −

Syntax to define Map Data Structure

#include 
map mapVariable;

We must import the "map" library to use the map data structure in this situation. Data types 1 and 2 are needed for this. The data type of the key parameter is type1, and the data type of the value parameter is type2. The object derived from the map type class is here mapVariable. Let's now see how to get this using C++ maps.

In the map data structure, we can put a value into the map by accessing an existing key or a new key. Since here we are talking about updating value, we must update an existing key. The key will be used inside square brackets like array indexing notations. Let us see the syntax for this −

Syntax to update elements inside the map

mapVariable [] = ;

Algorithm

  • An already created dictionary or map D

  • already existing key value k

  • the value v for the new key k

  • update like D[ k ] = v

  • return D

Example

#include <iostream> 
#include <map> 
using namespace std;

void display( map <string, int>& givenMap ){
   for ( auto& it : givenMap ) {
      cout << "Key: " << it.first << ", value: " << it.second << endl;
   }
}
   
int main(){ 
   map<string, int> givenMap;
   
   givenMap = { { "ABCD", 25 },
        { "EFGH", 50 },
        { "IJKL", 75 },
        { "MNOP", 100 },
        { "QRST", 125 }
   };
   
   cout << "Before updation: " << endl;
   display( givenMap );
  
   cout << "After Updation: " << endl;
   
   //update the value of MNOP to 500
   givenMap[ "MNOP" ] = 500; 
   display( givenMap );
}

Output

Before updation: 
Key: ABCD, value: 25
Key: EFGH, value: 50
Key: IJKL, value: 75
Key: MNOP, value: 100
Key: QRST, value: 125
After Updation: 
Key: ABCD, value: 25
Key: EFGH, value: 50
Key: IJKL, value: 75
Key: MNOP, value: 500
Key: QRST, value: 125

In this method, we have successfully updated a value by accessing the key parameter. However, this process may not be accurate all the time. There is a serious drawback of this process, where the given key may not exists in the map. But by using this process it will insert a new key with the given value. So in the next method, we will see how to search and update the element after a successful search.

Update after searching

Checking whether a key is present inside a map can be done using the find() function from the map object. It will return a pointer reference for the key, otherwise, it will return the ‘end()’ pointer for the map, which denotes the map does not contain the element inside it. Let us see the algorithm and implementation for a better understanding.

Algorithm

  • An already created dictionary or map D

  • already existing key value k

  • the value v for the new key k

  • create an iterator object itr to get the pointer for the key-value pair

  • call find() method for the dictionary D into itr

  • if itr is not the end of D, that signifies the key is present, then

    • put v into itr

  • end if

Example

#include <iostream> 
#include <map> 
using namespace std;

void display( map <string, int>& givenMap ){
   for ( auto& it : givenMap ) {
      cout << "Key: " << it.first << ", value: " << it.second << endl;
   }
}

void updateElement( map <string, int>& givenMap, string givenKey, int newValue ){
   map <string, int>::iterator itr;
   itr = givenMap.find( givenKey );
   if( itr != givenMap.end() ){   // when item has found
      itr->second = newValue;
   }
}
   
int main(){ 
   map<string, int> givenMap;
   
   givenMap = { { "ABCD", 25 },
        { "EFGH", 50 },
        { "IJKL", 75 },
        { "MNOP", 100 },
        { "QRST", 125 }
   };
   
   cout << "Before updation: " << endl;
   display( givenMap );
  
   cout << "After Updation: " << endl;
   
   //update the value of MNOP to 500
   updateElement( givenMap, "MNOP", 1580 );
   display( givenMap );
}

Output

Before updation: 
Key: ABCD, value: 25
Key: EFGH, value: 50
Key: IJKL, value: 75
Key: MNOP, value: 100
Key: QRST, value: 125
After Updation: 
Key: ABCD, value: 25
Key: EFGH, value: 50
Key: IJKL, value: 75
Key: MNOP, value: 1580
Key: QRST, value: 125

In this approach, the updateElement function is taking the map, the existing key, and the newValue as input. After that search for that key. If that is present then only update the value otherwise simply come out from the function. So by using this method, we cannot create a new entry into the map but only update an existing one.

Conclusion

In this post, we have seen how we can update elements in the map using a key. In the first approach, we are using a direct assigning method which is successfully updating the element but it can also add a new element when the key is not already present. The second approach removes this issue through a simple search at the beginning. Sometimes we may notice that the second method takes additional time to search for the key and then update it. So it is taking additional searching time than the first approach. But if we think carefully, in the first approach also the finding is being carried out inherently. Since the data structure used hashing-based techniques, it will operate in constant time (in most cases).

Updated on: 13-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements