- C++ Basics
- C++ Introduction
- C++ Installation
- C++ Syntax
- C++ Hello World
- C++ Comments
- C++ Variables
- C++ Data Types
- C++ Constants
- C++ Type Casting
- C++ Inline
- C++ File Inclusion
- C++ Date & Time
- C++ Return Types
- C++ Object Oriented
- C++ Classes
- C++ Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Exceptions
- C++ Advanced
- C++ Conditions
- C++ Loops
- C++ Functions
- C++ Structures
- C++ Enums
- C++ References
- C++ Pointers
- C++ Data Structures
- C++ Libs
- C++ Data Structures
- C++ Arrays
- C++ Vectors
- C++ Lists
- C++ Linked List
- C++ Deque
- C++ Stacks
- C++ Queues
- C++ Priority Queues
- C++ Sets
- C++ Maps
- C++ Unordered Sets and Maps
- C++ Graphs
C++ Maps
In C++, std::map is an associative container that stores key-value pairs in a sorted order based on the keys. Each key in a std::map is unique, and the values are associated with these keys. The std::map is implemented as a balanced binary search tree (usually a Red-Black Tree), which ensures that operations such as insertions, deletions, and lookups have logarithmic complexity.
Basic Operations with std::map
Include the Necessary Header:
cpp#include#include Declare a Map:
cppstd::map<int, std::string> myMap;This declares a map where the keys are of type
intand the values are of typestd::string.Add Elements:
cppmyMap[1] = "one"; myMap[2] = "two"; myMap[3] = "three";You can also use the
insertmethod to add elements:cppmyMap.insert(std::make_pair(4, "four"));Access Elements:
cppstd::cout << "Value associated with key 2: " << myMap[2] << std::endl;You can also use the
atmethod, which throws an exception if the key does not exist:cpptry { std::cout << "Value associated with key 5: " << myMap.at(5) << std::endl; } catch (const std::out_of_range& e) { std::cout << "Key 5 does not exist." << std::endl; }Check for Presence of a Key:
cppif (myMap.find(3) != myMap.end()) { std::cout << "Key 3 exists in the map." << std::endl; }Remove Elements:
cppmyMap.erase(2); // Removes the element with key 2Iterate Through the Map:
cppfor (const auto& pair : myMap) { std::cout << pair.first << ": " << pair.second << std::endl; }Get the Size of the Map:
cppstd::cout << "Size of the map: " << myMap.size() << std::endl;Check if the Map is Empty:
cppif (myMap.empty()) { std::cout << "Map is empty." << std::endl; }
Custom Comparator
You can specify a custom comparator to change the order of keys in the map:
Define a Custom Comparator:
cppstruct Compare { bool operator()(int a, int b) const { return a > b; // Descending order } };Declare a Map with the Custom Comparator:
cppstd::map<int, std::string, Compare> customMap;
Example Code
Here's a complete example demonstrating basic operations and a custom comparator:
cpp#include
#include
// Custom comparator for descending order
struct Compare {
bool operator()(int a, int b) const {
return a > b; // Descending order
}
};
int main() {
// Default map (ascending order)
std::map<int, std::string> myMap;
myMap[1] = "one";
myMap[2] = "two";
myMap[3] = "three";
std::cout << "Default map:" << std::endl;
for (const auto& pair : myMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
// Map with custom comparator (descending order)
std::map<int, std::string, Compare> customMap;
customMap[1] = "one";
customMap[2] = "two";
customMap[3] = "three";
std::cout << "Custom map (descending order):" << std::endl;
for (const auto& pair : customMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
// Check presence and remove elements
if (myMap.find(2) != myMap.end()) {
std::cout << "Key 2 exists in the default map." << std::endl;
}
myMap.erase(2);
std::cout << "After removing key 2, default map:" << std::endl;
for (const auto& pair : myMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
Output
yamlDefault map:
1: one
2: two
3: three
Custom map (descending order):
3: three
2: two
1: one
Key 2 exists in the default map.
After removing key 2, default map:
1: one
3: three
This example demonstrates the basic usage of std::map, including iteration, custom ordering, and element management.