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

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;
}
};

### Usingstd::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 {
}
}
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 {
}
}
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().

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.