How to Update Value of a Key in Map in C++? Last Updated : 09 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a map is a data structure that stores key-value pairs. In this article, we will learn how to change the value of a key that is already present in the map container in C++. Example Input: map<int, string> mp = {(1, "One"), (2,"Two")} Output: map after Updation: {(1, "One"), (2, "Three")}Change the Value of a Key in Map in C++To update the value of a key in the map, we can use the subscript operator [] to directly access and assign value. The key will be passed inside the [] and the assignment operator will be used to assign the new value. Keep in mind that the new value should be of the same type as the previous one. C++ Program to Update Value of a Key in Map C++ // C++ Program to Update Value of a Key in Map #include <iostream> #include <map> using namespace std; int main() { // creating a map map<int, string> myMap = { { 1, "One" }, { 2, "Two" } }; // Updating an existing element's value myMap[2] = "Three"; // Accessing and printing elements cout << "Map after Updating" << endl; for (auto it : myMap) { cout << "Key:" << it.first << " Value:" << it.second << endl; } return 0; } OutputMap after Updating Key:1 Value:One Key:2 Value:Three Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Update Value of a Key in Map in C++? S surya9c5sb Follow Improve Article Tags : C++ Programs C++ STL cpp-map CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Replace a Key-Value Pair in a Map in C++? A map in C++ is a part of the Standard Template Library (STL) that allows the user to store data in key-value pairs where each key in a map must be unique. In this article, we will learn how to replace a specific key-value pair in a Map in C++. Example: Input : map<int, string> mp={{1,"John"}, 2 min read How to Access a Value in a Map Using a Key in C++? In C++, std::map container store data in the form of key-value pairs where keys are associated with the given value. In this article, we will learn how to access a value in a std::map using key in C++.ExamplesInput: m = {{'a', 11}, {'b', 45}, {'c', 9}}, k = 'c'Output: 9Explanation: The key 'c' prese 3 min read How to swap Keys with Values of a Map in C++? Given a map, the task is to swap the Keys of this map with its values, in C++. Examples: Input: map = {'e', 1 }, {'o', 1 }, {'r', 3 }, Output: {1, 'e' }, {1, 'o' }, {3, 'r' }, Method 1: By using vector pair, traverse through the given map push_back the swapped values in the vector pair sort the vect 4 min read How to Find First Key-Value Pair in a Map in C++? In C++ STL, a map is a container that stores key-value pairs in an ordered or sorted manner. In this article, we will learn how to find the first key-value pair in a Map. Example: Input: myMap = {{1, "C++"}, {2, "Java"}, {3, "Python"}, {4, "JavaScript"}}; Output: First key-Value Pair : (1, C++)Getti 2 min read How to Remove a Key-Value Pair from Map in C++? In C++, a map is an associative container that stores elements in key-value pairs, where the key values are unique. In this article, we will learn how to remove a specific key-value pair from a map in C++. Example: Input : myMap = {{1, 10}, {3, 30}, {2, 20}, {4, 40}};key = 2Output :Map After Removal 2 min read Like