Home > Article > Backend Development > C++ program to update dictionary value by key
Many computer languages provide a dictionary, which is a data structure. Dictionary is a faster data structure that stores data based on keys and values. It preserves key-value combinations so that keys can be easily searched for certain components in near real-time. The C STL language standard includes dictionary-like data structures. The term "map" is used to describe this data structure. This map creates a pair of keys and values of any type (since we are using C, the types must be defined before compilation). This section demonstrates how to update values in an existing map or dictionary in C.
Let’s first look at the definition of map data structure. These internal templates require two different types. The syntax and necessary libraries are shown below -
#include <map> map<type1, type2=""> mapVariable; </type1,></map>
In this case, we must import the "map" library to use the map data structure. Data types 1 and 2 are required for this. The data type of the key parameter is type1, and the data type of the value parameter is type2. Here the object derived from the map type class is mapVariable. Now let's see how to get it using a C map.
In a map data structure, we can put values into the map by accessing existing keys or new keys. Since we are talking about updating values here, we have to update the existing key. Keys will be used within square brackets like array index notation. Let's look at the syntax of this -
mapVariable [<the key="" value="">] = <the new="" value="">; </the></the>
Created dictionary or map D
Existing key value k
The value of new key k v
Update like D[ k ] = v
Return D
#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 ); }
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 successfully updated the value by accessing the key parameter. However, this process may not always be accurate. This procedure has a serious drawback, which is that the given key may not exist in the map. But by using this process it will insert new key with given value. So in the next method we will see how to search and update an element after a successful search.
You can use the find() function in the map object to check whether a key exists in the map. It will return a pointer reference to the key, otherwise it will return the "end()" pointer to the map, which indicates that the map does not contain an element within it. Let us see the algorithm and implementation for better understanding.
Created dictionary or map D
Existing key value k
The value of new key k v
Create an iterator object itr to obtain the pointer of the key-value pair
Call the find() method to put the dictionary D into itr
If itr is not the end of D, it means the key exists, then
Put v into itr
End if
#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 ); }
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 method, the updateElement function takes as input the map, the existing key, and newValue. Then search for that key. Just update the value if it exists, otherwise just derive it from the function. So by using this method we cannot create new entries in the map but can only update existing entries.
In this article, we learned how to update elements in a map using keys. In the first method, we use the direct assignment method, which successfully updates the element, but it can also add new elements when the key does not exist yet. The second method eliminates this problem by starting with a simple search. Sometimes we may notice that the second method takes extra time to search for the key and then update it. Therefore, it requires more search time than the first method. But if we think carefully, in the first method, this discovery is also essentially realized. Since the data structure uses hash-based techniques, it will run in constant time (in most cases).
The above is the detailed content of C++ program to update dictionary value by key. For more information, please follow other related articles on the PHP Chinese website!