find_if<>: Sequential Search, Customizable

Live Hacking

if, Using a Plain Function

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>


bool match_666(int i)
{
    return i == 666;
}
bool match_42(int i)
{
    return i == 42;
}

int main()
{
    // find_if on a plain old C array
    {
        int array[] = { 34, 45, 1, 3, 2, 666 };

        // find/search the number 666 (for example)

        // nobody wants index based iteration
        for (int i=0; i<6; i++) {
            if (array[i] == 666) {
                cout << "yay, found at position " << i << endl;
                break;
            }
        }

        // nobody wants pointer arithmetic done manually

        const int* run = array;
        const int* end = array + 6;

        while (run < end) {
            if (*run == 666) {
                cout << "yay, found at position " << run << endl;
                break;
            }
            run++;
        }
        cout << *run << endl;


        // some want to use std::find_if
        const int* found = std::find_if(array, array+6, match_666);
        cout << *found << endl;
    }

    // now for STL vector
    {
        // nobody wants old-style vector "initialize"
        {
            std::vector<int> array;
            array.push_back(34);
            array.push_back(45);
            array.push_back(1);
            array.push_back(3);
            array.push_back(2);
            array.push_back(666);
        }

        // everybody wants *brace initialization* (since C++11)
        std::vector<int> array = { 34, 45, 1, 3, 2, 666 };

        std::vector<int>::const_iterator found = std::find_if(array.begin(), array.end(), match_666);
        cout << *found << endl;

        found = std::find_if(array.begin(), array.end(), match_42);
        if (found == array.end())
            cout << "not found" << endl;
        else
            cout << *found << endl;
    }

    return 0;
}

if, Using a Functor Class

And C++11 lambdas?

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>


class Point
{
public:
    Point(int x, int y) : _x(x), _y(y) {}
    int x() const { return _x; }
    int y() const { return _y; }
private:
    int _x;
    int _y;
};


bool x_equals_666(const Point& p)
{
    if (p.x() == 666)
        return true;
    else
        return false;
}

bool x_equals_4(const Point& p)
{
    if (p.x() == 4)
        return true;
    else
        return false;
}

class x_equal
{
public:
    x_equal(int criterion) : _criterion(criterion) {}
    bool operator()(const Point& p) const
    {
        if (p.x() == _criterion)
            return true;
        else
            return false;
    }
private:
    int _criterion;
};

int main()
{
    std::vector<Point> points;
    points.push_back(Point(1, 2));
    points.push_back(Point(2, 3));
    points.push_back(Point(4, 5));
    points.push_back(Point(666, 42));
    points.push_back(Point(666, 43));

    // for (size_t i=0; i<points.size(); i++) {
    //     if (points[i].x() == 666) {
    //         cout << '(' << points[i].x() << ',' << points[i].y() << ')' << endl;
    //         break;
    //     }
    // }

    // for (size_t i=0; i<points.size(); i++) {
    //     bool yesno = x_equals_666(points[i]);
    //     if (yesno) {
    //         cout << '(' << points[i].x() << ',' << points[i].y() << ')' << endl;
    //         break;
    //     }
    // }

    // std::vector<Point>::const_iterator found = std::find_if(points.begin(), points.end(), x_equals_666);
    // if (found != points.end())
    //     cout << '(' << found->x() << ',' << found->y() << ')' << endl;

    // found = std::find_if(points.begin(), points.end(), x_equals_4);
    // if (found != points.end())
    //     cout << '(' << found->x() << ',' << found->y() << ')' << endl;

    std::vector<Point>::const_iterator found = std::find_if(points.begin(), points.end(), x_equal(666));
    if (found != points.end())
        cout << '(' << found->x() << ',' << found->y() << ')' << endl;

    found = std::find_if(points.begin(), points.end(), x_equal(4));
    if (found != points.end())
        cout << '(' << found->x() << ',' << found->y() << ')' << endl;

    return 0;
}