C++ std::map value_comp() Function

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

Definition

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()));
CallWhat 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

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

Return Value

Return ValueDescription
value_compareA 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;
}

How It Works

  1. The map holds 1 -> "zebra" and 2 -> "ant". By key the order is 1 then 2, but by value "ant" would come first.
  2. vc(*first, *second) compares the keys 1 and 2 and returns true, so the values are ignored.
  3. 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.

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

int main() {
    std::map<std::string, int> m = {{"pear", 7}, {"apple", 50}, {"kiwi", 1}};

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

    std::cout << "biggest key: " << it->first << " (value " << it->second << ")\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 map are pairs, so m.value_comp() fits.
  2. The comparison looks only at keys, in alphabetical order: apple, kiwi, pear. The biggest of them is "pear".
  3. The value of apple (50) is the largest value, but it is ignored. That is why the answer is pear and its value 7.

Example 3 — value_comp() Ignores the Value

Two pairs with the same key are equivalent for the map, even if the values differ.

C++
#include <iostream>
#include <map>
#include <string>
#include <utility>

int main() {
    std::map<int, std::string> m;
    auto vc = m.value_comp();

    std::pair<const int, std::string> a(1, "b");
    std::pair<const int, std::string> b(1, "a");

    std::cout << std::boolalpha;
    std::cout << "vc(a, b): " << vc(a, b) << '\n';
    std::cout << "vc(b, a): " << vc(b, a) << '\n';
    std::cout << "std::pair a < b: " << (a < b) << '\n';
    return 0;
}

How It Works

  1. Both pairs have the key 1. Their values differ: "b" and "a".
  2. 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.
  3. 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.

Back to the function index

See all 32 std::map 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.

11 people found this page helpful