Search Algorithms

Using std::ranges::find() with Custom Data Types

How can I use std::ranges::find() with custom data types that don't implement the equality operator ==?

Abstract art representing computer programming

To use std::ranges::find() with custom data types that don't implement the equality operator ==, you need to define this operator for your custom type.

The find() algorithm relies on this operator to compare elements.

Defining the Equality Operator

Let's say you have a custom data type called MyStruct:

struct MyStruct {
  int Value;
};

To make MyStruct work with std::ranges::find(), you need to define the equality operator:

struct MyStruct {
  int Value;
  bool operator==(const MyStruct& Other) const {
    return Value == Other.Value; 
  }
};

Using std::ranges::find() with Custom Data Types

Now, you can use std::ranges::find() with a std::vector of MyStruct:

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

struct MyStruct {
  int Value;
  bool operator==(const MyStruct& Other) const {
    return Value == Other.Value;
  }
};

int main() {
  std::vector<MyStruct> Numbers{
    {1}, {2}, {3}, {4}, {5}
  };

  MyStruct target{3};
  auto result = std::ranges::find(Numbers, target);

  if (result != Numbers.end()) {
    std::cout << "Found struct with value "
      << result->Value << "\n";
  } else {
    std::cout << "Struct not found\n";
  }
}
Found struct with value 3

Alternative: Custom Comparison Function

If you cannot modify the custom type to include the equality operator, you can use std::ranges::find_if() with a custom comparison function:

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

struct MyStruct {
  int Value;
};

bool compare(const MyStruct& a, int b) {
  return a.Value == b;
}

int main() {
  std::vector<MyStruct> Numbers{
    {1}, {2}, {3}, {4}, {5}
  };

  auto result = std::ranges::find_if(Numbers,
    [](const MyStruct& obj) {
      return compare(obj, 3); 
  });

  if (result != Numbers.end()) {
    std::cout << "Found struct with value "
      << result->Value << "\n";
  } else {
    std::cout << "Struct not found\n";
  }
}
Found struct with value 3

By defining the equality operator or using std::ranges::find_if() with a custom comparison function, you can effectively search custom data types that don't implement the equality operator ==.

This Question is from the Lesson:

Search Algorithms

An introduction to the 8 main searching algorithms in the C++ standard library, including find(), find_if(), find_if_not(), find_first_of(), adjacent_find(), search_n(), search(), and find_end().

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

This Question is from the Lesson:

Search Algorithms

An introduction to the 8 main searching algorithms in the C++ standard library, including find(), find_if(), find_if_not(), find_first_of(), adjacent_find(), search_n(), search(), and find_end().

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