Maps are associative containers that store data as sorted pairs of keys and values. It is an implementation of Self-Balancing Binary Search Tree, specifically a Red-Black Tree Which ensures,
- Maps allow searching, insertion, and deletion and take O(log n) time.
- Maps automatically avoid duplicate keys.
- Keys are stored in ascending order.
- Compared to unordered_map, the times taken for search, insert and delete are more in map, but we get items in sorted order and it supports additional functions like upper_bound() and lower_bound().
Let us take a look at an example to create a map and traverse it.
C++
#include <iostream>
#include <map>
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
Syntax
The map container is defined as std::map class template inside the <map> header file.
map<key_type, value_type> m;
where,
- key_type: Data type of key.
- value_type: Data type of value.
- m: Name assigned to map.
Basic Operations
Basic operations on map containers are shown below:
Inserting Elements
- The insert() operation adds a new key-value pair to the map only if the key is not already present.
- If the key exists, insert() does not update the value and leaves the map unchanged.
- Time complexity to insert is O(log n).
C++
#include <iostream>
#include <map>
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
Accessing Elements
- We can access elements with [] operator which returns the value for a given key and inserts the key with a default value if it doesn't exist.
- To check if a key exists without adding it by we can use find().
- Time complexity to access elements by key is O(log n).
C++
#include <iostream>
#include <map>
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;
}
Updating Elements
- To update a value, we can simply assign a new value to an existing key using map[key]= newValue; If the key already exists , the value gets updated.
- Time complexity to update element by key O(log n).
C++
#include <iostream>
#include <map>
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;
}
Finding Elements
- find() function is used to check if key exists in a map which looks for key and returns its position if found.
- If key is not present in the map, find() returns a special value called end(), meaning not found.
- Time complexity to find element by key is O(log n).
C++
#include <iostream>
#include <map>
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;
}
Traversing
- Loops can be used to traverse all key-value pairs in a map, which visits each pair in order sorted by the keys.
- Time complexity to traverse in a map is O(n).
C++
#include <iostream>
#include <map>
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
Deleting Elements
- To delete a key and its value from map use erase(key), which deletes the pair if the key exists, else does nothing.
- Time complexity to delete an element by key O(log n).
C++
#include <iostream>
#include <map>
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;
}
Explore
Introduction to C++
Basics
Core Concepts
C++ OOP
Standard Template Library(STL)
Practice C++
Top C++ DSA Related Problems