How to Declare a Map in C++? Last Updated : 28 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values. In this article, we will learn how to declare a map in C++. How to declare a map in C++?In C++, you can declare a map using the std::map template class, which is part of the <map> header. Syntax to declare a map in C++ STLstd::map<KeyType, ValueType> mapName;where, KeyType: This is the data type of the keys in the map.ValueType: This is the data type of the values associated with the keys in the map.mapName: This is the name you give to your map variable.C++ Program to Declare a Map C++ // C++ Program to declare a map #include <iostream> #include <map> #include <string> using namespace std; // Driver Code int main() { // Create a map of strings to integers map<string, int> mp; // Insert some values into the map mp["one"] = 1; mp["two"] = 2; mp["three"] = 3; // Get an iterator pointing to the first element in the // map map<string, int>::iterator it = mp.begin(); // Iterate through the map and print the elements while (it != mp.end()) { cout << "Key: " << it->first << ", Value: " << it->second << endl; ++it; } return 0; } OutputKey: one, Value: 1 Key: three, Value: 3 Key: two, Value: 2 Comment More infoAdvertise with us Next Article How to Declare a Map in C++? 21mcsrltd Follow Improve Article Tags : C++ Programs C++ cpp-map CPP Examples Practice Tags : CPP Similar Reads How to Declare a Vector in C++? In C++, the vector is a dynamic array that can resize itself automatically to accommodate new elements. In this article, we will learn how to declare a vector in C++. Declare a Vector in C++In C++, vectors are defined as the class templates in <vector> header file. So, to declare a std::vector 2 min read How to Add Elements to a Map in C++? 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, "Moh 2 min read How to Create a Stack of Map in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::map is an associative container that stores key-value pairs. In this article, we will learn how to create a stack of a map in C++. Example:Input: myMap = { {1: âaâ}, {2: âbâ}, {3: âcâ} }; myMap = { {4 2 min read How to Create a Map of Arrays in C++? In C++, the std::map is a container that stores elements in a key-value pair, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a map of arrays in C++. Example: Input: arr1 = {1, 2, 3};arr2 = {4, 5, 6};arr3 = {7, 8, 9}; 2 min read How to Create a Deque of Maps in C++? In C++, the Standard Template Library (STL) provides a container called deque that allows efficient insertion and deletion operations at both ends of the container. A map is an associative container that stores elements in key-value pairs.In this article, we will learn how to create a deque of maps 2 min read Like