11

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;
});
1
  • 1
    continue is a reserved word and can be used only inside loop. In second example you are using continue inside function.
    – Vitalii
    Jun 19, 2019 at 14:29

1 Answer 1

19

Replace continue with return.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.