I am using algorithms from std::ranges
(max
and max_element
) with a projection.
Is it possible for the result to also be the projected value? Currently I have to call the projection function again on the returned value.
Example: Here I want the size of the longest string, but the algorithms return only the string or an iterator to it.
int main()
{
const std::vector<std::string> vec = {
"foo",
"hello",
"this is a long string",
"bar"
};
//r1 is a string. r2 is an iterator
const auto r1 = std::ranges::max(vec, {}, &std::string::size);
const auto r2 = std::ranges::max_element(vec, {}, &std::string::size);
//I have to call size() again
std::cout << r1 << '\n' << *r2 << '\n';
std::cout << r1.size() << '\n' << r2->size() << std::endl;
}