c++ - 在特定条件下跳过 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;
});
解决方案
替换continue
为return
。
推荐阅读
- maven - 如何编译 epublib
- javascript - “(AppContainer)RangeError:超出最大调用堆栈大小” - 与反应热加载器反应
- razor - 在自定义标签助手中写入内容
- php - 在 linux cli 中执行我的 php 代码的延迟
- swift - 尝试启动 dataTask 时出现 URLError(_nsError: The operation could not be completed) 错误
- c - ISO C11 标准中的左值
- node.js - 节点串口无法读取数据
- swift - 如何在 swift 中编写正确的聚合类
- asp.net-core - 通过 OneLogin 和 .NET Core 使用 OpenID Connect/OAuth2 进行基于角色的访问控制
- android - 为什么带有 Chip 的 marginEnd 的 HorizontalScrollView 内的 ChipGroup 不起作用?