In C++, std::map::value_comp() returns a comparison object for whole pairs. You give it two pairs, such as *it1 and *it2, and it tells you whether the first pair goes before the second.
The name is misleading. It does not compare the stored values. It looks at the keys of the two pairs and uses the map's key comparison. It is handy for algorithms like std::max_element that work on pairs. Include <map> to use it.
This is a member of std::map<Key, T>, a sorted container of key and value pairs. Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax
Call value_comp to get the object, then call it with two pairs.
C++
auto vc = m.value_comp();
bool before = vc(*m.begin(), *std::next(m.begin()));
Call
What it does
m.value_comp()
Returns the value_compare object of the map.
m.value_comp()(p1, p2)
Compares two pairs by their keys. Returns true if the key of p1 goes before the key of p2.
value_comp()(p1, p2) is the same as key_comp()(p1.first, p2.first). It exists so that you can pass the whole pair to algorithms that compare elements.
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 pairs by key.
C++ Examples
Example 1 — value_comp() Compares Pairs by Key
Compare two pairs whose values are in the opposite order of their keys.
C++
#include <iostream>
#include <iterator>
#include <map>
#include <string>
int main() {
std::map<int, std::string> m = {{1, "zebra"}, {2, "ant"}};
auto vc = m.value_comp();
auto first = m.begin();
auto second = std::next(m.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 map holds 1 -> "zebra" and 2 -> "ant". By key the order is 1 then 2, but by value "ant" would come first.
vc(*first, *second) compares the keys 1 and 2 and returns true, so the values are ignored.
The reverse call returns false. If value_comp() compared values, the two answers would be swapped.
Example 2 — value_comp() with std::max_element
Pass value_comp() to an algorithm so that it can compare whole pairs.
Both pairs have the key 1. Their values differ: "b" and "a".
vc(a, b) and vc(b, a) are both false, because the keys are equal and the values are not looked at. A map would treat them as the same key.
The plain operator< of std::pair also compares the second member. Here "b" < "a" is false, so it prints false. For b < a it would say true, which shows the difference.
Frequently Asked Questions
A function object that compares two pairs (value_type) by their keys, using the map's key comparison.
No. Despite its name it compares only the keys of the two pairs. The mapped values are ignored.
key_comp() takes two keys. value_comp() takes two whole pairs, but then compares their keys.
When you pass pairs from the map to an algorithm that needs a comparison, such as std::max_element, std::min_element, or std::is_sorted.
Use std::max_element with your own lambda that compares a.second and b.second.