|
| 1 | +--- |
| 2 | +Title: '.key_comp' |
| 3 | +Description: 'Returns a copy of the comparison function object used internally by associative containers (like `std::map`) to order their keys.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Developer Tools' |
| 7 | +Tags: |
| 8 | + - 'Data' |
| 9 | + - 'Data Structures' |
| 10 | + - 'Map' |
| 11 | + - 'STL' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-plus-plus' |
| 14 | + - 'path/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`.key_comp()`** function returns a copy of the comparison function object (typically `key_compare`) used by the container to order its keys. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +myMap.key_comp(); |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `.key_comp()` takes no parameters. |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +- Returns the key comparison function object used by the map to order its keys. |
| 32 | + - By default, this is `std::less<Key>` unless a custom comparator was specified when defining the map. |
| 33 | + |
| 34 | +## Example |
| 35 | + |
| 36 | +This is an example demonstrating how to use `.key_comp()` to compare keys in a `std::map`: |
| 37 | + |
| 38 | +```cpp |
| 39 | +#include <iostream> |
| 40 | +#include <map> |
| 41 | + |
| 42 | +int main() { |
| 43 | + |
| 44 | + std::map<int, std::string> myMap = { |
| 45 | + {1, "Apple"}, |
| 46 | + {2, "Banana"}, |
| 47 | + {3, "Orange"}, |
| 48 | + {4, "Date"}, |
| 49 | + {5, "Mango"} |
| 50 | + }; |
| 51 | + |
| 52 | + auto comp = myMap.key_comp(); |
| 53 | + |
| 54 | + int keyToCompare = 3; |
| 55 | + |
| 56 | + for (const auto& pair : myMap) { |
| 57 | + if (comp(pair.first, keyToCompare)) { |
| 58 | + std::cout << pair.first << " is less than " << keyToCompare << std::endl; |
| 59 | + } else if (comp(keyToCompare, pair.first)) { |
| 60 | + std::cout << pair.first << " is greater than " << keyToCompare << std::endl; |
| 61 | + } else { |
| 62 | + std::cout << pair.first << " is equal to " << keyToCompare << std::endl; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return 0; |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +The output of this code will be: |
| 71 | + |
| 72 | +```shell |
| 73 | +1 is less than 3 |
| 74 | +2 is less than 3 |
| 75 | +3 is equal to 3 |
| 76 | +4 is greater than 3 |
| 77 | +5 is greater than 3 |
| 78 | +``` |
| 79 | + |
| 80 | +## Codebyte Example |
| 81 | + |
| 82 | +This is a codebyte example demonstrating how to use `.key_comp()` to compare keys in a `std::map`: |
| 83 | + |
| 84 | +```codebyte/cpp |
| 85 | +#include <iostream> |
| 86 | +#include <map> |
| 87 | +
|
| 88 | +struct CustomCompare { |
| 89 | + bool operator()(int lhs, int rhs) const { |
| 90 | + return lhs > rhs; // Reverse order |
| 91 | + } |
| 92 | +}; |
| 93 | +
|
| 94 | +int main() { |
| 95 | + std::map<int, char, CustomCompare> myMap = {{1, 'a'}, {2, 'b'}, {3, 'c'}}; |
| 96 | +
|
| 97 | + auto comp = myMap.key_comp(); |
| 98 | +
|
| 99 | + std::cout << "Comparing 2 and 3: " << comp(2, 3) << std::endl; |
| 100 | + std::cout << "Comparing 3 and 2: " << comp(3, 2) << std::endl; |
| 101 | +
|
| 102 | + return 0; |
| 103 | +} |
| 104 | +``` |
0 commit comments