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 ==?

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 ==.

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

Questions & Answers

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

Case-Insensitive Search Using std::ranges::find_if()
How can I make std::ranges::find_if() case-insensitive when searching through a container of strings?
Finding the Last Occurrence of an Element in a Container
How do I search for the last occurrence of an element using std::ranges::find() or std::find()?
Searching for Multiple Occurrences of a Value in a Container
What is the best way to search for multiple occurrences of a value in a container using standard library algorithms?
Searching for a Substring Appearing Multiple Times
How do I handle searching for a substring within a string when the substring can appear multiple times?
Boyer-Moore vs Boyer-Moore-Horspool Search Algorithms
What are the differences and benefits of using std::boyer_moore_searcher versus std::boyer_moore_horspool_searcher in practical scenarios?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant