Minimum and Maximum Algorithms

Use cases for std::ranges::minmax()

What are some real-world applications of std::ranges::minmax()?

Abstract art representing computer programming

std::ranges::minmax() is a powerful function that can be used in various real-world applications where you need to find both the minimum and maximum elements efficiently. Here are some practical examples:

1. Normalizing Data

In data science and statistics, normalizing data to a specific range is common. You can use std::ranges::minmax() to find the minimum and maximum values in a dataset, which helps in scaling the data.

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

int main() {
  std::vector<int> data { 10, 20, 30, 40, 50 };
  auto [min, max] = std::ranges::minmax(data);

  std::cout << "Min: " << min
    << ", Max: " << max << "\n";

  for (int& value : data) {
    value = (value - min) * 100 / (max - min); 
    std::cout << value << " ";
  }
}
Min: 10, Max: 50
0 25 50 75 100

2. Game Development

In game development, you might need to determine the player with the highest and lowest scores. std::ranges::minmax() can be used to quickly find these players.

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

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

int main() {
  std::vector<Player> players {
    {"Alice", 500},
    {"Bob", 300},
    {"Charlie", 800}
  };

  auto [minPlayer, maxPlayer] = std::ranges::minmax(
    players, [](const Player& a, const Player& b) {
      return a.score < b.score;
    }
  );

  std::cout << "Lowest score: "
    << minPlayer.score
    << " (" << minPlayer.name << ")\n";

  std::cout << "Highest score: "
    << maxPlayer.score
    << " (" << maxPlayer.name << ")\n";
}
Lowest score: 300 (Bob)
Highest score: 800 (Charlie)

3. Financial Analysis

In financial analysis, you might need to find the lowest and highest prices of a stock over a period. std::ranges::minmax() helps in identifying these critical points efficiently.

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

int main() {
  std::vector<double> stockPrices {
    150.5, 200.0, 180.3, 220.1, 190.4 };
  auto [minPrice, maxPrice] =
    std::ranges::minmax(stockPrices);

  std::cout << "Lowest price: $" << minPrice
    << "\nHighest price: $" << maxPrice;
}
Lowest price: $150.5
Highest price: $220.1

Summary

std::ranges::minmax() is versatile and can be applied in various domains such as data science, game development, and financial analysis.

It efficiently finds both the minimum and maximum elements in a range, which can be used for normalizing data, analyzing player scores, and identifying critical financial metrics.

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

A computer programmer
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.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved