Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip Iteration in std::for_each on specific condition

Tags:

c++

I'm working on a program that needs to iterate over a range. I wish to know if I can use continue like when I use in range based for loop.

Working :

std::vector<std::string> v = {"foo", "bar", "baz", "foobar"};
for (auto s : v)
{
    if (*s.front() == 'b')
        continue;
    std::cout << s << std::endl;
}

Not-Working :

std::vector<std::string> v = {"foo", "bar", "baz", "foobar"};
std::for_each(v.begin(), v.end(), [](const std::string& s) {
    if (*s.front() == 'b')
        continue;
    std::cout << s << std::endl;
});
like image 375
Dark Sorrow Avatar asked May 21 '19 06:05

Dark Sorrow


1 Answers

Replace continue with return.

like image 182
bipll Avatar answered Sep 28 '22 03:09

bipll