C++
std::find
error
debugging
programming

stdfind 'error no matching function'

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

When C++ reports that std::find has “no matching function,” the problem is usually not std::find itself. It usually means one of the arguments does not match what the algorithm expects: missing headers, wrong iterator types, mismatched element and search value types, or attempting to use std::find where std::find_if is actually needed. The fix is to verify the call signature first and then inspect the types involved.

What std::find Expects

The normal shape is:

cpp
1#include <algorithm>
2#include <vector>
3#include <iostream>
4
5int main() {
6    std::vector<int> values {1, 2, 3, 4};
7
8    auto it = std::find(values.begin(), values.end(), 3);
9
10    if (it != values.end()) {
11        std::cout << "found\n";
12    }
13}

The three arguments are:

  • iterator to the beginning of the range
  • iterator to the end of the range
  • value to compare with elements in that range

If your call does not fit that shape, the compiler’s template deduction may fail.

Common Cause: Missing #include <algorithm>

The simplest cause is forgetting the algorithm header.

cpp
#include <vector>
// #include <algorithm>

Without <algorithm>, the compiler may not see the correct declaration for std::find at all. This is the first thing to check.

Common Cause: Value Type Does Not Match the Container

Suppose the container stores std::string but you search with a type that does not compare cleanly.

cpp
1#include <algorithm>
2#include <string>
3#include <vector>
4
5int main() {
6    std::vector<std::string> names {"ava", "mia"};
7    auto it = std::find(names.begin(), names.end(), "ava");
8}

This often works because a string literal can convert to std::string for comparison, but in other cases the mismatch is not so friendly. A more obviously broken example would be searching a container of complex objects with a raw field value.

If the container holds custom types, std::find compares whole elements, not individual fields.

Use std::find_if for Custom Conditions

If you want to find an object by a property, use std::find_if.

cpp
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5struct User {
6    int id;
7};
8
9int main() {
10    std::vector<User> users {{1}, {2}, {3}};
11
12    auto it = std::find_if(users.begin(), users.end(), [](const User& u) {
13        return u.id == 2;
14    });
15
16    if (it != users.end()) {
17        std::cout << "found user " << it->id << "\n";
18    }
19}

Trying to use std::find(users.begin(), users.end(), 2) would fail because a User is not directly comparable to an int.

Iterator Range Problems

Both iterators must come from the same container and must be valid iterator types.

Bad idea:

cpp
// std::find(a.begin(), b.end(), value);

Even if the compiler accepts some invalid combinations elsewhere, mixing iterators from different containers is logically wrong and leads to undefined behavior or type errors.

Arrays and Pointers

std::find also works on raw arrays if you pass pointers to the start and one-past-the-end location.

cpp
1#include <algorithm>
2#include <iostream>
3
4int main() {
5    int data[] = {10, 20, 30};
6
7    auto it = std::find(data, data + 3, 20);
8
9    if (it != data + 3) {
10        std::cout << "found\n";
11    }
12}

If you accidentally pass the wrong end pointer, the problem may not be a matching-function error, but it will still be a bug.

Equality Operator Requirements

std::find uses equality comparison. For custom types, that usually means the type needs a valid operator== if you want whole-object search.

cpp
1#include <algorithm>
2#include <vector>
3
4struct Point {
5    int x;
6    int y;
7
8    bool operator==(const Point& other) const {
9        return x == other.x && y == other.y;
10    }
11};
12
13int main() {
14    std::vector<Point> points {{1, 2}, {3, 4}};
15    auto it = std::find(points.begin(), points.end(), Point{3, 4});
16}

Without a usable equality comparison, std::find cannot compare elements correctly.

Read the Deduced Types

Compiler messages for templates can be noisy, but the useful part is often the deduced type mismatch. Ask:

  • what is the container element type
  • what type is the value argument
  • am I searching by whole value or by condition

Once those three are clear, the correct algorithm usually becomes obvious.

Common Pitfalls

The most common mistake is forgetting <algorithm>. Another is using std::find when the problem actually requires std::find_if with a predicate. Developers also often search containers of custom objects without defining equality or without matching the search value type to the element type. Finally, template error output can tempt you into changing random syntax when the real fix is just to inspect the types carefully.

Summary

  • 'std::find expects a begin iterator, an end iterator, and a comparable value.'
  • Include <algorithm> before using it.
  • Use std::find_if when you need to search by a property or condition.
  • Ensure the search value is compatible with the container element type.
  • For custom types, provide equality or switch to a predicate-based search.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.