To sort a map by keys in C++, you can use the default behavior of the `std::map` container, which stores key-value pairs in sorted order based on the keys. This method ensures that the map is always sorted by keys.
We can sort a map by keys in C++ by using the `std::map` container, which automatically sorts the keys.
For example,
<map>
header file, which provides the necessary functions and data structures for working with maps.<iostream>
header for input-output operations.myMap
with some key-value pairs. In this example, the map has integer keys and string values.std::cout
function to verify the sorting.#include <map>
#include <iostream>
int main() {
// Declare and initialize a map
std::map<int, std::string> myMap = {
{3, "three"},
{1, "one"},
{2, "two"}
};
// Print the sorted map
std::cout << "Sorted Map by Keys: \n";
for (const auto& pair : myMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
Sorted Map by Keys: 1: one 2: two 3: three
We can sort a map by keys in descending order in C++ by using `std::map` with a custom comparator.
For example,
<map>
and <functional>
header files, which provide the necessary functions and data structures for working with maps and custom comparators.<iostream>
header for input-output operations.myMap
with a custom comparator that sorts the keys in descending order.std::cout
function to verify the sorting.#include <map>
#include <functional>
#include <iostream>
int main() {
// Declare and initialize a map with a custom comparator for descending order
std::map<int, std::string, std::greater<int>> myMap = {
{3, "three"},
{1, "one"},
{2, "two"}
};
// Print the sorted map
std::cout << "Sorted Map by Keys (Descending): \n";
for (const auto& pair : myMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
Sorted Map by Keys (Descending): 3: three 2: two 1: one
We can sort a map by keys in C++ using a custom comparator function with `std::map` to specify a specific sorting order.
For example,
<map>
and <functional>
header files, which provide the necessary functions and data structures for working with maps and custom comparators.<iostream>
header for input-output operations.customComparator
that sorts the keys in a specific order.myMap
with the custom comparator function.std::cout
function to verify the sorting.#include <map>
#include <functional>
#include <iostream>
// Custom comparator function
struct customComparator {
bool operator()(const int& lhs, const int& rhs) const {
return lhs > rhs; // Example: descending order
}
};
int main() {
// Declare and initialize a map with the custom comparator function
std::map<int, std::string, customComparator> myMap = {
{3, "three"},
{1, "one"},
{2, "two"}
};
// Print the sorted map
std::cout << "Sorted Map by Keys (Custom Order): \n";
for (const auto& pair : myMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
Sorted Map by Keys (Custom Order): 3: three 2: two 1: one
In this tutorial, we learned How to Sort a Map by Keys in C++ language with well detailed examples.