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.

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().

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

clamp() with custom types
How can I use clamp() with custom types?
Using minmax_element() with a lambda
How can I use minmax_element() with a lambda function for comparison?
Use cases for std::ranges::minmax()
What are some real-world applications of std::ranges::minmax()?
How do I find the second smallest or second largest element?
How do I find the second smallest or second largest element using standard library algorithms?
Changing comparison criteria at runtime
Can I use standard library algorithms with different comparison criteria at runtime?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant