c++ find object in vector by attribute

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

struct MyObject {
    int id;
    std::string name;

    MyObject(int i, const std::string& n) : id(i), name(n) {}
};

bool findObjectById(const MyObject& obj, int targetId) {
    return obj.id == targetId;
}

int main() {
    std::vector<MyObject> myVector;

    // Populate the vector with some objects
    myVector.push_back(MyObject(1, "Object1"));
    myVector.push_back(MyObject(2, "Object2"));
    myVector.push_back(MyObject(3, "Object3"));

    // Specify the target ID to find
    int targetIdToFind = 2;

    // Use std::find_if to find the object with the specified ID
    auto it = std::find_if(myVector.begin(), myVector.end(), [targetIdToFind](const MyObject& obj) {
        return findObjectById(obj, targetIdToFind);
    });

    // Check if the object was found
    if (it != myVector.end()) {
        std::cout << "Object found: " << it->name << std::endl;
    } else {
        std::cout << "Object not found." << std::endl;
    }

    return 0;
}