C++ std::set key_comp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::set

Definition

In C++, std::set::key_comp() returns a copy of the comparison object that the set uses to put its values in order. You call that object like a function with two values, and it says whether the first value comes before the second.

On a set the “key” is the value itself. The default comparison is std::less<T>, which gives ascending order. You can choose another one, such as std::greater, when you create the set. Include <set> to use it.

This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.

Syntax

Call key_comp to get the comparison object, then call that object with two values.

C++
auto comp = s.key_comp();
bool before = comp(1, 2);           // true for std::less
bool same = s.key_comp()(5, 5);     // false
CallWhat it does
s.key_comp()Returns the key_compare object of the set.
s.key_comp()(a, b)Calls the comparison on two values. Returns true if a goes before b in the set.

Two values a and b are treated as equal by the set when neither goes before the other: !comp(a, b) && !comp(b, a). The set never uses == on values.

Library declarations
C++
key_compare key_comp() const;

Parameters

ParameterDescription
Nonekey_comp() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
key_compareA copy of the comparison object used to order the values. Usually std::less<T>.

C++ Examples

Example 1 — key_comp() Compares Values

Get the comparison object of a default set and call it on two values.

C++
#include <iostream>
#include <set>

int main() {
    std::set<int> s;
    auto comp = s.key_comp();

    std::cout << std::boolalpha;
    std::cout << "comp(1, 2): " << comp(1, 2) << '\n';
    std::cout << "comp(2, 1): " << comp(2, 1) << '\n';
    std::cout << "comp(5, 5): " << comp(5, 5) << '\n';
    return 0;
}

How It Works

  1. s.key_comp() returns the comparison object. The set has no values, but the comparison still exists, because it is part of the set type.
  2. The default is std::less<int>, so comp(1, 2) asks “is 1 < 2?” and the answer is true. comp(2, 1) is false.
  3. comp(5, 5) is false, because a value does not go before itself. This is the rule that makes values unique.

Example 2 — key_comp() with std::greater

Create a set that sorts values from big to small, and see the comparison change.

C++
#include <functional>
#include <iostream>
#include <set>

int main() {
    std::set<int, std::greater<int>> s = {1, 3, 2};

    std::cout << std::boolalpha;
    std::cout << "comp(1, 2): " << s.key_comp()(1, 2) << '\n';
    std::cout << "comp(2, 1): " << s.key_comp()(2, 1) << '\n';

    for (int v : s) {
        std::cout << v << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The second template argument std::greater<int> tells the set to put bigger values first.
  2. Now key_comp()(1, 2) asks “is 1 > 2?”, which is false. key_comp()(2, 1) asks “is 2 > 1?”, which is true.
  3. The comparison drives the order of the whole set, so the loop prints 3 2 1.

Example 3 — key_comp() Defines When Values Are Equal

Use a custom comparator that ignores letter case. Two spellings become the same value.

C++
#include <algorithm>
#include <cctype>
#include <iostream>
#include <set>
#include <string>

struct NoCase {
    bool operator()(const std::string& a, const std::string& b) const {
        return std::lexicographical_compare(
            a.begin(), a.end(), b.begin(), b.end(),
            [](unsigned char x, unsigned char y) { return std::tolower(x) < std::tolower(y); });
    }
};

int main() {
    std::set<std::string, NoCase> s;
    s.insert("Apple");
    s.insert("APPLE");

    auto comp = s.key_comp();
    bool equivalent = !comp("Apple", "APPLE") && !comp("APPLE", "Apple");

    std::cout << "size: " << s.size() << '\n';
    std::cout << *s.begin() << '\n';
    std::cout << "equivalent: " << std::boolalpha << equivalent << '\n';
    return 0;
}

How It Works

  1. NoCase compares two strings letter by letter after lowering the case, so "Apple" and "APPLE" do not go before each other.
  2. The second insert("APPLE") finds the existing value because the set sees them as equal. Nothing new is stored, so the size stays 1 and the first spelling remains.
  3. key_comp() returns a NoCase object. If comp(a, b) and comp(b, a) are both false, the values are equivalent, which prints true.

Frequently Asked Questions

A copy of the comparison object the set uses to order its values. By default it is std::less<T>.
Give the set std::greater as its second template argument: std::set<int, std::greater<int>>. key_comp() then returns that comparison.
It treats values a and b as equal when neither comp(a, b) nor comp(b, a) is true. It does not use operator==.
On a set they compare the same element type. key_comp() takes two keys (the values). value_comp() takes two value_type elements, which for a set are also the values.
No. The comparison is part of the set type and is fixed when the set is created.

Next: lower_bound()

Keep going through the std::set functions. Find the first value that is not less than the given value.

lower_bound() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful