In Python, given a list, I can sort it by a key function, e.g.:

```
>>> def get_value(k):
...     print "heavy computation for", k
...     return {"a": 100, "b": 30, "c": 50, "d": 0}[k]
...
>>> items = ['a', 'b', 'c', 'd']
>>> items.sort(key=get_value)
heavy computation for a
heavy computation for b
heavy computation for c
heavy computation for d
>>> items
['d', 'b', 'c', 'a']

```

As you see, the list was sorted not alphanumerically but by the return value of `get_value()`.

Is there an equivalent in C++? `std::sort()` only allows me to provide a custom comparator (equivalent of Python's `items.sort(cmp=...)`), not a key function. If not, is there any well-tested, efficient, publicly available implementation of the equivalent I can drop into my code?

Note that the Python version only calls the `key` function once per element, not twice per comparison.