我正在开发一个需要迭代一定范围的程序。我想知道是否可以continue在基于范围的for循环中使用时使用。
工作方式:
std::vector<std::string> v = {"foo", "bar", "baz", "foobar"};
for (auto s : v)
{
if (*s.front() == 'b')
continue;
std::cout << s << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
不工作:
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;
});
Run Code Online (Sandbox Code Playgroud)