Minimum and Maximum Algorithms

std::ranges::min_element() vs std::ranges::min()

What are the differences between std::ranges::min_element() and std::ranges::min()?

While std::ranges::min_element() and std::ranges::min() may seem similar at first glance, they serve different purposes and return different types of results. Understanding these differences can help you choose the right tool for yourÂ needs.

std::ranges::min() returns the minimum value directly. If the range is empty, it throws anÂ exception.

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers{3, 1, 4, 1, 5};
int minValue = std::ranges::min(numbers);
std::cout << "Minimum value: " << minValue;
}
Minimum value: 1

std::ranges::min_element() returns an iterator to the minimum element in the range. If the range is empty, it returns last.

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers{3, 1, 4, 1, 5};
auto minIt = std::ranges::min_element(numbers);
if (minIt != numbers.end()) {
std::cout << "Minimum element: " << *minIt;
}
}
Minimum element: 1

Use Cases

• Direct Value Comparison: Use std::ranges::min() when you need the minimum value directly and do not need to know its position in the collection.
• Position of Minimum Element: Use std::ranges::min_element() when you need the iterator to the minimum element, which allows you to modify or further interact with the collection at that position.

Performance Considerations

Both functions perform a linear scan of the range, but their performance impact can differ based on your use case. For instance, std::ranges::min_element() might be more efficient if you only need the position of the minimum element and plan to make changes to theÂ collection.

Example

Imagine a scenario where you have a list of players, and you want to find the player with the least health and then increase theirÂ health:

#include <algorithm>
#include <iostream>
#include <vector>

struct Player {
std::string name;
int health;
};

int main() {
std::vector<Player> players {
{"Alice", 100},
{"Bob", 50},
{"Charlie", 150}
};

auto minIt = std::ranges::min_element(players,
[](const Player& a, const Player& b) {
return a.health < b.health;
});

if (minIt != players.end()) {
minIt->health += 10;
std::cout << minIt->name << " now has "
<< minIt->health << " health.";
}
}
Bob now has 60 health.

Summary

• std::ranges::min() returns the minimum value.
• std::ranges::min_element() returns an iterator to the minimum element.
• Choose based on whether you need the value directly or the position of the element in the collection.

Understanding these differences helps in selecting the right algorithm for your specificÂ requirements.

This Question is from the Lesson:

Minimum and Maximum Algorithms

An introduction to the seven minimum and maximum algorithms in the C++ standard library: clamp(), min(), min_element(), max(), max_element(), minmax(), and minmax_element().

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Minimum and Maximum Algorithms

An introduction to the seven minimum and maximum algorithms in the C++ standard library: clamp(), min(), min_element(), max(), max_element(), minmax(), and minmax_element().

Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

• 124 Lessons
• 550+ Code Samples
• 96% Positive Reviews
• Regularly Updated
• Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.