DEV Community

Discussion on: Getting Cozy With C++

Collapse
 
dwd profile image
Dave Cridland

As a long-time C++ developer, this is absolutely spot on. One change you might wish to make:

If having std::find is forcing you into defining an operator==() that you didn't otherwise want, you might want to look at std::find_if, which takes a UnaryPredicate - basically a functor accepting a single value. Conveniently, this can be a lambda.

So you'd do:

std::find_if(vec.begin(), vec.end(), [&t](Cell const & other) {
    return t.row == other.col && t.col == other.col;
}

It achieves the same thing (and, likely, with the same CPU instructions), but you've kept the required definition of equality for this purpose next to the point of use, instead of defining it globally.

Collapse
 
deciduously profile image
Ben Lovy

Ah, perfect! That does make sense, I'll probably refactor.

Collapse
 
banzyme2 profile image
ENDEESA

Typo: t.row == other.col