In C++, maps are associative containers that store data in the form of key value pairs sorted on the basis of keys. No two mapped values can have the same keys. By default, it stores data in ascending order of the keys, but this can be changes as per requirement.
Example:
C++ #include <bits/stdc++.h> using namespace std; int main() { // Creating a map of integer keys // and string values map<int, string> m {{1, "Geeks"}, {2,"For"}, {3,"Geeks"}}; for (auto& p : m) cout << p.first << " " << p.second << "\n"; return 0; }
Output1 Geeks 2 For 3 Geeks
Explanation: In the above program, we created a map m of integer keys and string values. We inserted three key-value pairs into the map: {1, "Geeks"}, {2, "For"}, and {3, "Geeks"}. The map automatically sorts the keys in ascending order.
Syntax
The map container is defined as std::map class template inside the <map> header file.
map<key_type, value_type, comp> m;
where,
- key_type: Data type of key.
- value_type: Data type of value.
- comp: Custom comparator function that defines how to compare two keys for sorting. It is optional and if not provided, sorts data in increasing order of the keys.
- m: Name assigned to map.
Declaration and Initialization
We can declare and initialize a map in different ways as shown in the below example:
C++ #include <bits/stdc++.h> using namespace std; int main() { // Creating an empty map map<int, string> m1; // Initialze map with list map<int, string> m2 = {{1, "Geeks"}, {2, "For"}, {3, "Geeks"}}; for (auto& p : m2) cout << p.first << " " << p.second << endl; return 0; }
Output1 Geeks 2 For 3 Geeks
Example: In the above program,
- Statement map<int, string> m1 is an empty map with no elements.
- Statement map<int, string> m2 = {{1, "Geeks"}, {2, "For"}, {3, "Geeks"}} initialized to three key-value pairs using initializer list.
To see more ways to declare and initialize map, refer to this article - Different Ways to Initialize a Map
Basic Operations
Basic operations on map containers are shown below:
1. Inserting Elements
Elements can be inserted into a map using either [] operator or insert() method. If the element with the given key already exists, the insert() method skips the insertion but [] operator updates the associated value to the new value.
Example:
C++ #include <bits/stdc++.h> using namespace std; int main() { map<int, string> m = {{2, "For"}, {3, "Geeks"}}; // Inserting a key value pair m.insert({1, "Geeks"}); for (auto x: m) cout << x.first << " " << x.second << endl; return 0; }
Output1 Geeks 2 For 3 Geeks
We cannot specify any particular position to insert element as map automatically sort the data according to the order. To know more ways to insert elements in a map, refer this article - Different Ways to Insert Elements in a Map
2. Accessing Elements
Map elements can be accessed by using the corresponding key inside operator []. If the key exists, it will return the associated value but if the key doesn't exist, it will create a new element with the given key and the default value. To avoid this, we can also use at() method for accessing elements without any modification.
Example:
C++ #include <bits/stdc++.h> using namespace std; int main() { map<int, string> m = {{1, "Geeks"}, {2, "For"}, {3, "Geeks"}}; // Accessing elements cout << m[1] << endl; cout << m.at(2); return 0; }
To know more methods to access values in a map, refer to the article - Different Ways to Access a Value in a Map
3. Updating Elements
The key of an already present elements cannot be modified in the map. But the associated value can be changed by first accessing the element and then using assignment operator to change the value.
Example:
C++ #include <bits/stdc++.h> using namespace std; int main() { map<int, string> m = {{1, "Geeks"}, {2, "For"}, {3, "Geeks"}}; // Updating value m[0] = "Tweaks"; m.at(1) = "By"; cout << m[0] << endl; cout << m.at(1); return 0; }
Explanation: In this program, expression m[0] = "Tweaks" updates the value associated with the key 0. Similarly, expression m.at(1) = "By" updates the value of the key 1.
To see more methods to update values in a map, refer to the article - Different Ways to Update Value of a Pair in Map
4. Finding Elements
Map provides fast element search by key using the find() member function. This function returns iterator the element if found, otherwise returns end() iterator.
Example:
C++ #include <bits/stdc++.h> using namespace std; int main() { map<int, string> m = {{1, "Geeks"}, {2, "For"}, {3, "Geeks"}}; // Finding element with key 2 auto it = m.find(2); if (it != m.end()) cout << it->first << " " << it->second; else cout << "Key not Found!"; return 0; }
To know more methods to search element in map, refer to this article - Check if Map Contains a Specific Key
5. Traversing
Maps can be easily traversed by using either range based for loop or using begin() and end() iterator with traditional loops.
Example:
C++ #include <bits/stdc++.h> using namespace std; int main() { map<int, string> m = {{1, "Geeks"}, {2, "For"}, {3, "Geeks"}}; // Traversing using iterators for (auto it = m.begin(); it != m.end(); ++it) cout << it->first << " " << it->second << endl; return 0; }
Output1 Geeks 2 For 3 Geeks
To see more methods to traverse a map, refer to the article - Different Ways to Traverse a Map
6. Deleting Elements
Map elements can be deleted from a map using erase() method by passing the key or an iterator.
Example:
C++ #include <bits/stdc++.h> using namespace std; int main() { map<int, string> m = {{1, "Geeks"}, {2, "For"}, {3, "Geeks"}}; // Deleting by key m.erase(2); // Deleting by iterator m.erase(m.begin()); for(auto i : m) cout << i.first << " " << i.second << endl; return 0; }
To know more ways to delete elements in a map, refer this article - Different Ways to Delete Elements from Map
Time Complexity
The below table lists the time complexity of the above operations on map:
Operation | Time Complexity |
---|
Insert an element | O(log n) |
Delete an element by key | O(log n) |
Access element by key | O(log n) |
Find an element by key | O(log n) |
Update element by key | O(log n) |
Traverse the map | O(n) |
Other Common Operations
Following are some other commonly used operations on a map in C++:
Internal Working
In C++, map is an associative container that provides the built-in implementation of Red-Black Tree. It stores the elements in some sorted order on the basis of keys. Due do RB Trees, insertion, deletion, and search operations takes logarithmic O(log n) time.
All Member Functions
Here's the list of all member functions of std::map:
Function | Description |
---|
insert() | Insert elements with a particular key in the map container. |
---|
count() | Returns the number of elements matching specific key |
---|
equal_range() | Returns an iterator of pairs. The pair refers to the bounds of a range that includes all the elements in the container which have a key equivalent to k. |
---|
erase() | Used to erase elements from the map. |
---|
begin() | Returns an iterator pointing to the first element of the map. |
---|
end() | Returns an iterator pointing to the first element of the map. |
---|
rend() | Returns a reverse iterator pointing to the element preceding the first element of the map |
---|
rbegin() | Returns a reverse iterator pointing to the last element of the map. |
---|
find() | Returns an iterator to the element with key-value in the map if found, else returns the iterator to end. |
---|
crbegin() | crbegin() returns a constant reverse iterator referring to the last element in the map container. |
---|
crend() | crend() returns a constant reverse iterator pointing to the theoretical element before the first element in the map. |
---|
cbegin() | cbegin() returns a constant iterator referring to the first element in the map container. |
---|
cend() | cend() returns a constant iterator pointing to the element that is beyond the last element. |
---|
emplace() | Inserts the key with value in the map container. |
---|
max_size() | Returns the maximum number of elements a map can hold. |
---|
upper_bound() | Find the first element in the map that is just greater than the given key. |
---|
lower_bound() | Find the first element in the map that is equal to or greater than the given key. |
---|
emplace_hint() | Inserts the key and its element in the map container with a given hint. |
---|
value_comp() | Returns the object that determines how the elements in the map are ordered ('<' by default). |
---|
key_comp() | Returns the object that determines how the elements in the map are ordered ('<' by default). |
---|
size() | Returns the number of elements in the map. |
---|
empty() | Returns whether the map is empty. |
---|
clear() | Removes all the elements from the map. |
---|
at() | at() function is used to the element associated with the key k. |
---|
swap() | swap() function is used to exchange the contents of two maps but the maps must be of the same type, although sizes may differ. |
---|
Similar Reads
Map in C++ STL In C++, maps are associative containers that store data in the form of key value pairs sorted on the basis of keys. No two mapped values can have the same keys. By default, it stores data in ascending order of the keys, but this can be changes as per requirement.Example:C++#include <bits/stdc++.h
8 min read
Different Ways to Initialize a Map in C++ Initializing map refers to the process of assigning the initial values to the elements of map container. In this article, we will learn different methods to initialize the map in C++. Let's start from the easiest method:Using Initializer ListThe simplest way to initialize an std::map container is by
3 min read
Inserting Elements in a Map in C++ In C++, map stores unique key value pairs in sorted order and provides fast insert, delete and search operation. In this article, we will learn different methods to insert an element in a map in C++.The recommended method to insert an element in a map is by using map insert() method. Letâs take a lo
4 min read
Different ways to delete elements in std::map (erase() and clear()) This article deals with the deletion part of Maps. We can delete elements in std::map using two functions 1. Using erase() The erase() is used to erase the pair in the map mentioned in the argument, either its position, its value, or a range of numbers. We can use the erase function in the following
4 min read
Commonly Used Methods
Other Member Methods
Map and External Sorting Criteria/Comparator in C++ STL C++ Map is another commonly used STL container, it stores elements in a mapped fashion. Each element is stored as a pair having a key value and a mapped value. No two mapped values can have the same key values which means each key is unique. By default, key values in the map are in lexicographically
4 min read
Get Map Element at Offset in C++ STL Prerequisites: Map in C++ STL Since the map is not indexed as arrays or vectors sequential access is not possible in the map but to access an element at a particular offset. Example: inserted elements are { 'a',11 } , { 'b',12 } , { ' c ', 13 } then we can get { 'b', 12 } for 2 position. Methods to
3 min read
Search by value in a Map in C++ Given a set of N pairs as a (key, value) pairs in a map and an integer K, the task is to find all the keys mapped to the given value K. If there is no key value mapped to K then print "-1".Examples: Input: Map[] = { {1, 3}, {2, 3}, {4, -1}, {7, 2}, {10, 3} }, K = 3 Output: 1 2 10 Explanation: The 3
2 min read
Passing Map as Reference in C++ STL Prerequisite: Maps in C++ STL Pass by reference Elements in the map are in form of pairs where the first is key and another value, denoting key-value pairs. Also, all the key values are unique means no two elements can have the same key value. Passing maps by value is a costly task, costly in terms
3 min read