- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ multiset Library - key_comp() Function
Description
The multiset::key_comp() function in C++ STL returns a copy of the comparison object which is used by the multiset to compare keys. The returned comparison object defines the ordering of the elements in the multiset.
Syntax
Following is the syntax of multiset::key_comp() function −
key_compare multiset_name.key_comp() const;
Parameters
The key_comp() function does not accept any parameters.
Return value
The key_comp() function returns a copy of the comparison object used by the multiset. By default, it uses the less than(<) operator for comparison.
Exceptions
If an exception is thrown, the container remains unchanged.
Time complexity
The time complexity of multiset::key_comp() function is O(1) (constant time).
Examples of multiset::key_comp() Function
The following examples demonstrate the usage of multiset::key_comp() function in multiset −
Using key_comp() to Compare Elements
Below is an example to compare two integers (num1 and num2) in multiset using the comparison object returned by multiset::key_comp() function. The (comp(num1, num2)) checks num1 < num2.
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> numbers = {10, 20, 30, 40, 50};
cout << "Multiset elements:";
for (const auto &num : numbers)
cout << " " << num;
cout << endl;
auto comp = numbers.key_comp();
int num1 = 25, num2 = 35;
cout << "Comparing " << num1 << " with " << num2 << ":\n";
if (comp(num1, num2))
cout << num1 << " is less than " << num2 << endl;
else
cout << num1 << " is not less than " << num2 << endl;
cout << "\nComparing " << num2 << " with " << num1 << ":\n";
if (comp(num2, num1))
cout << num2 << " is less than " << num1 << endl;
else
cout << num2 << " is not less than " << num1 << endl;
return 0;
}
The output of the above code is given below −
Multiset elements: 10 20 30 40 50 Comparing 25 with 35: 25 is less than 35 Comparing 35 with 25: 35 is not less than 25
Printing Elements Less Than or Greater Than a Specific Key
You can use the comparison object returned by key_comp() to print all elements in the multiset that are less than or greater than a specific element. Here, we have printed all the elements that are less than and greater than the limit, which is 40.
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> nums = {10, 40, 20, 30, 40, 50, 90, 35};
cout << "Keys in multiset:";
for (const auto &num : nums)
cout << " " << num;
cout << endl;
auto comp = nums.key_comp();
int limit = 40;
cout << "Keys less than " << limit << " are: ";
for (auto it = nums.begin(); it != nums.end(); ++it)
{
if (comp(*it, limit))
cout << *it << " ";
else
break;
}
cout << endl;
cout << "Keys greater than " << limit << " are: ";
for (auto it = nums.begin(); it != nums.end(); ++it)
{
if (comp(limit, *it))
cout << *it << " ";
}
cout << endl;
return 0;
}
The output of the code is given below −
Keys in multiset: 10 20 30 35 40 40 50 90 Keys less than 40 are: 10 20 30 35 Keys greater than 40 are: 50 90
Using key_comp() to Compare Strings
In this example, we have compared two string elements of multiset using the key_comp() function −
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
multiset<string> names = {"Aryan", "Bob", "Viper", "David"};
cout << "Strings in Multiset: ";
for (const auto &name : names)
cout << " " << name;
cout << endl;
auto comp = names.key_comp();
cout << "Checking if 'Viper' comes before 'Aryan': ";
if (comp("Viper", "Aryan"))
cout << "Yes, Viper comes before Aryan" << endl;
else
cout << "No, Aryan comes before Viper" << endl;
return 0;
}
The output of the above code is given below −
Strings in Multiset: Aryan Bob David Viper Checking if 'Viper' comes before 'Aryan': No, Aryan comes before Viper