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()));
Call
What 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
Parameter
Description
None
value_comp() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
value_compare
A 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;
}
Output
first before second: true
second before first: false
How It Works
The set holds 2, 5, and 9 in ascending order. The first two iterators point at 2 and 5.
vc(*first, *second) asks whether 2 goes before 5. With the default std::less, the answer is true.
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;
}
Output
biggest: pear
How It Works
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.
The comparison uses alphabetical order: apple, kiwi, pear. The biggest of them is "pear".
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.