Thiago R. Adams website

Home Blog Code-Blog Twitter Downloads Links / Books About

Websites

Fun with lambdas - for_each_where


template<class IT, class F, class W>
F for_each_where(IT i, IT e, W w, F f)
{
    for (; i != e; ++i)
    {
        if (w(*i))
        {
            f(*i);
        }
    }
    return (f);
}

int main()
{
    std::vector<Point> v;
    v.push_back(Point(1, 1));
    v.push_back(Point(2, 1));
    v.push_back(Point(3, 1));


    for_each_where(v.begin(), v.end(),
                   [](Point& p) { return p.x > 1; }, // condition
                   [](Point& p) { cout << p.x; });   // code
}

Want to see more? Go to the CodeBlog section.

About the author: I am Thiago Adams. I work as a professional C++ software engineer. I have created this website to share ideas and source code with other people with similar interests.
I would like to hear from you comments, critics, questions and suggestions about this topic or any other part of this website. Email: thiago.adams at gmail dot com