How to Add Elements to a Map in C++? Last Updated : 22 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a map is an associative container that stores the elements as key-value pairs where each element has a key and a mapped value. In this article, we will learn how to add elements to a map in C++ STL. For Example, Input: myMap = {{1, "Ram"}, {2, "Mohit"}}; Output: myMap = {{1, "Ram"}, {2, "Mohit"}, {3: "Sam"}}; Add Elements into a Map in C++To insert a key-value pair in a std::map, we can use the array subscript operator [] with the key inside the brackets and assign it the required value using the assignment operator. If the key already exists, then its value will be updated. Otherwise, the new elements will be added C++ Program to Add Key-Value Pair into a Map The below example demonstrates how we use insert() function to add key-value pair into a map in C++ STL. C++ // C++ program to add key-value pair in map in STL #include <iostream> #include <map> using namespace std; int main() { // Create a map map<int, string> myMap; // Insert key-value pairs into the map myMap[1] = "Apple"; myMap[2] = "Banana"; myMap[3] = "Cherry"; // Print the map cout << "The map is: \n"; for (const auto& pair : myMap) { cout << pair.first << ": " << pair.second << "\n"; } return 0; } OutputThe map is: 1: Apple 2: Banana 3: Cherry Time Complexity: O(m log n), where n is the number of elements in the map, and m is the number of elements to be inserted.Auxiliary Space: O(m) Note: We can also use map::insert()ethod to add key-value pairs in a map in C++. Comment More infoAdvertise with us Next Article How to Add Elements to a Map in C++? S sireeshakanneganti112 Follow Improve Article Tags : C++ Programs C++ STL cpp-map CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Add Elements in a Vector in C++? In C++, vector provides several built-in methods to insert the elements and efficiency of the insertion depends on the position where the insertion takes place. In this article, we will learn different ways to insert elements into a vector in C++ and also compare their efficiency.The simplest way to 3 min read How to Add an Element to a Set in C++? In C++ STL, a set is a container that stores unique elements in a sorted order. In this article, we will learn how to add an element to a set in C++ STL. Example: Input: mySet = {1, 2, 4, 5, 8} Element to add: 3 Output: mySet = {1, 2, 3, 4, 5, 8}Add an Element to a Set in C++To add a specific elemen 2 min read How to Add Multiple Elements to a Set in C++? In C++, a set is a container that stores the unique elements in a sorted order. In this article, we will learn how to add multiple elements to a set in C++. For Example, Input: mySet = {1, 5, 6, 7, 8} Elements to add: {1, 2, 3, 4, 5} Output: Set Elements are: 1 2 3 4 5Insert Multiple Elements in a S 2 min read How to Access Elements of Set of Maps in C++? In C++, a set of maps can be used to store multiple maps of key-value pairs sorted according to their number and values of elements such as graphs. In this article, we will explore how we can access the elements of a set of maps in C++. Example: Input: { { {1: "C++"}, {2:" Python"} }, { {3:" Java"}, 2 min read How to Add Element to Vector of Pairs in C++? In C++, vectors are dynamic arrays that can grow and shrink in size whereas a pair is a container that can store two data elements or objects. A vector of pairs, therefore, is a dynamic array of pairs. In this article, we will learn how to add an element to a vector of pairs in C++. Example: Input:v 2 min read Like