How to Replace a Key-Value Pair in a Map in C++? Last Updated : 29 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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,"Bob"}, {3,"Alice"}}; keytoReplace = 3; valuetoReplace = "Sam"; Output: Original Map: {1: John} {2: Bob} {3: Alice} Updated Map: {1: John} {2: Bob} {3: Sam} Replace a Key-Value Pair in a Map in C++To replace a specific key-value pair in a map in C++, we can use the array subscript [] operator to access the value associated with the specific key and then directly replace that key in the map using assignment operator. C++ Program to Replace a Key-Value Pair in a Map in C++The below example demonstrates how we can replace a specific key-value pair in a map in C++. C++ // C++ Program to illustrate how to Replace a Specific // Key-Value Pair in a Map #include <iostream> #include <map> using namespace std; int main() { // creating and Initializing a map map<int, string> mp = { { 1, "John" }, { 2, "Bob" }, { 3, "Alice" } }; // Print the original map cout << "Original Map:" << endl; for (auto pair : mp) { cout << "{" << pair.first << ": " << pair.second << "} "; } cout << endl; // Replace key value pairs in the map int keyToReplace = 3; mp[keyToReplace] = "Sam"; cout << "Updated Map: " << endl; // Print the updated map for (auto pair : mp) { cout << "{" << pair.first << ": " << pair.second << "} "; } return 0; } OutputOriginal Map: {1: John} {2: Bob} {3: Alice} Updated Map: {1: John} {2: Bob} {3: Sam} Time Complexity: O(logN), where N is the size of the mapAuxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Replace a Key-Value Pair in a Map in C++? A abhiisaxena09 Follow Improve Article Tags : C++ Programs C++ STL cpp-map CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 How to Initialize a Map with a Range of Key-Value Pairs in C++? In C++, a map is an associative container that stores elements formed by combining a key and a value. In this article, we will learn how to initialize a map with a range of key-value pairs in C++. Initializing a Map with a Range of Key-Value Pairs in C++To initialize a std::map with a range of key-v 2 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 Replace a Value in an Array in C++? In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to replace a value in an array in C++. Example: Input: arr: {10 20 30 30 20 10 10 20} Element_to_replace= 20 replacement=99 Output: Arr 2 min read How to Update Value of a Key in Map in C++? 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") 2 min read How to Delete a Key-Value Pair from a Map in C++? In C++, maps are used to store key-value pairs in which each key is unique. In this article, we will learn how to delete a key-value pair from a map in C++. Example Input: mp={ {1,"One"}, {2,"Two"},{3,"Three"}}Key= 2Output: Map after deleting key: 1: One3: ThreeRemove a Key-Value Pair from Map in C+ 2 min read How to Replace a Specific Pair in a Multimap in C++? in C++, multimap is similar to a map that stores the data in the key-value format where duplicate keys are allowed. In this article, we will learn how to replace a specific pair in a multimap in C++. Example Input: myMultimap = {{1, âoneâ}, {2, âtwoâ}, {2, âtwoâ}, {3, âthreeâ}}; Key-Value Pair to Re 3 min read How to Add Multiple Key-Value Pairs to a Map in C++? In C++, a map is a container that stores elements in key-value pairs, where each key is unique and associated with a value. In this article, we will learn how to add multiple key-value pairs to a map in C++. Example: Input: myMap = { {1, "one"} }; Elements to be insert = { {2, "two"}, {3, "three"}} 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 Store Vectors as Keys in a Multimap in C++? In C++, the std::multimap is a container that stores elements in a key-value pair, whereas std::vector is a sequence container that stores elements in contiguous memory. In this article, we will learn how to store vectors as keys in a multimap in C++. Example: Input:myVector ={1,2,3};myVector ={4,5, 2 min read Like