The Reduce and Accumulate Algorithms

Reduce and Mixed Data Types

Can std::reduce() handle input with mixed data types?

Abstract art representing computer programming

std::reduce() is designed to work with a range of elements that are generally of the same type, but it can handle mixed data types with some considerations.

The key is that the binary operation used in std::reduce() must be able to accept and return the appropriate types.

If you have a collection of mixed data types, you need to ensure that the operation you provide can handle these types correctly.

One common approach is to use a std::variant or a custom struct to encapsulate the different types.

Here's an example using a custom struct to handle a mix of integers and floats:

#include <iostream>
#include <numeric>
#include <variant>
#include <vector>

struct Mixed {
  int intValue;
  float floatValue;
};

Mixed addMixed(const Mixed& a, const Mixed& b) {
  return {
    a.intValue + b.intValue,
    a.floatValue + b.floatValue
  };
}

int main() {
  std::vector<Mixed> numbers{
    {1, 1.1f}, {2, 2.2f}, {3, 3.3f}};

  Mixed result = std::reduce(
    numbers.begin(),
    numbers.end(),
    Mixed{0, 0.0f},
    addMixed
  );

  std::cout << "Int Result: "
    << result.intValue << "\n";
  std::cout << "Float Result: "
    << result.floatValue << '\n';
}
Int Result: 6
Float Result: 6.6

In this example, Mixed is a struct that holds both an integer and a float. The addMixed function defines how two Mixed objects should be combined.

When calling std::reduce(), we pass Mixed{0, 0.0f} as the initial value and addMixed as the binary operation.

For simpler cases, if the mixed types are compatible with basic arithmetic operations, you can use std::variant with a lambda to handle the combination:

#include <iostream>
#include <numeric>
#include <variant>
#include <vector>

using Mixed = std::variant<int, float>;

float addMixed(float a, const Mixed& b) {
  return a + std::visit([](auto&& value) {
    return static_cast<float>(value);
  }, b);
}

int main() {
  std::vector<Mixed> numbers{1, 2.2f, 3};

  float result = std::reduce( numbers.begin(),
    numbers.end(), 0.0f, addMixed);

  std::cout << "Result: " << result;
}
Result: 6.2

This approach uses std::variant to handle different types and std::visit() to apply the correct operation within the lambda.

In summary, std::reduce() can handle mixed data types, but you must ensure that the provided operation can manage those types correctly.

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:

  • 125 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