C++ std::set value_comp() Function

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

Definition

In C++, std::set::value_comp() returns a comparison object for whole elements. You give it two values from the set, such as *it1 and *it2, and it tells you whether the first goes before the second.

On a set, value_type is the same as the key type (as const T). So value_comp() and key_comp() compare the same kind of thing. It is handy for algorithms like std::max_element. 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 value_comp to get the object, then call it with two elements.

C++
auto vc = s.value_comp();
bool before = vc(*s.begin(), *std::next(s.begin()));
CallWhat it does
s.value_comp()Returns the value_compare object of the set.
s.value_comp()(a, b)Compares two elements. Returns true if a goes before b.

For a set, value_comp()(a, b) is the same as key_comp()(a, b). It exists so that generic code written for maps and sets can use one name that always takes a value_type.

Library declarations
C++
value_compare value_comp() const;

Parameters

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

Return Value

Return ValueDescription
value_compareA function object that compares two value_type elements.

C++ Examples

Example 1 — value_comp() Compares Set Values

Compare the first two values of a sorted set.

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

int main() {
    std::set<int> s = {2, 5, 9};
    auto vc = s.value_comp();

    auto first = s.begin();
    auto second = std::next(s.begin());

    std::cout << std::boolalpha;
    std::cout << "first before second: " << vc(*first, *second) << '\n';
    std::cout << "second before first: " << vc(*second, *first) << '\n';
    return 0;
}

How It Works

  1. The set holds 2, 5, and 9 in ascending order. The first two iterators point at 2 and 5.
  2. vc(*first, *second) asks whether 2 goes before 5. With the default std::less, the answer is true.
  3. The reverse call returns false. The comparison matches the order you already see when you loop the set.

Example 2 — value_comp() with std::max_element

Pass value_comp() to an algorithm so that it can compare whole elements.

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

int main() {
    std::set<std::string> s = {"pear", "apple", "kiwi"};

    auto it = std::max_element(s.begin(), s.end(), s.value_comp());

    std::cout << "biggest: " << *it << '\n';
    return 0;
}

How It Works

  1. std::max_element needs a comparison that works on the elements of the range. The elements of a set are the values themselves, so s.value_comp() fits.
  2. The comparison uses alphabetical order: apple, kiwi, pear. The biggest of them is "pear".
  3. You already know the last element is at std::prev(s.end()). The example shows how algorithms can reuse the set's own comparison.

Example 3 — value_comp() Matches key_comp()

Show that both observers give the same answers on a set.

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

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

    std::cout << std::boolalpha;
    std::cout << "kc(3, 8): " << kc(3, 8) << '\n';
    std::cout << "vc(3, 8): " << vc(3, 8) << '\n';
    std::cout << "same: " << (kc(3, 8) == vc(3, 8)) << '\n';
    return 0;
}

How It Works

  1. Both observers return comparison objects for the set's order.
  2. On a set the key type and the value type are the same kind of element, so both objects accept two ints.
  3. The answers match. Prefer key_comp() when you think in terms of keys, and value_comp() when you pass elements to a generic algorithm.

Frequently Asked Questions

A function object that compares two elements (value_type) using the set's comparison.
They compare the same kind of element. The names exist so that map and set share a common interface.
When you pass set elements to an algorithm that needs a comparison, such as std::max_element, std::min_element, or std::is_sorted.
No. It only asks questions. The order is fixed by the comparator chosen when the set was created.
Yes. You can call it with any two values of the element type, even if those values are not currently stored.

Back to the function index

See all 29 std::set member functions grouped by what they do.

Function index →

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