首页 > 解决方案 > 在特定条件下跳过 std::for_each 中的迭代

问题描述

我正在开发一个需要在一个范围内迭代的程序。我想知道continue在基于范围的 for 循环中使用时是否可以使用 like。

在职的 :

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

不工作:

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;
});

标签: c++

解决方案


替换continuereturn


推荐阅读