C++ replace_if在 STL 中带有结构的向量上
C++ replace_if on vector with struct in STL
我一直在寻找一种使用 std::replace_if 更新向量中当前结构的方法,而不是创建新对象。
例如
std::vector<setting> settings;
// ... add stuff to settings
setting newsetting;
std::string name = "something";
std::replace_if(settings.begin(), settings.end(), [&](setting const& v)
{
return v.name == name;
}, newsetting);
在这种情况下,我需要使用 newset,有什么方法可以引用我现有的向量并更新其中的值?我已经查找了这个问题,但只在 boost 中找到了解决方案,我想严格在 STL 中执行此操作。
谢谢。
在我看来,
你不需要比花园品种范围迭代更花哨的东西:
std::vector<setting> settings;
for (auto &one_setting:settings)
{
if (one_setting.name == name)
{
one_setting.a_method_that_changes_this_setting_somehow();
}
}
相关文章:
- 将结构向量排序为子组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 如何在 c++ 中创建结构向量的映射
- 带unique_ptr的结构向量
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么
- C++对结构向量进行排序
- 在C++中对结构向量进行排序时出现问题
- 将结构向量保存到文件中,并从C++文件中读取结构向量
- 搜索并替换结构向量中的值
- 在结构向量中查找最接近的值
- 清除结构向量时会发生什么?
- 如何从 node-ffi 调用 c++ 中以结构向量作为参数的方法?
- 对结构向量进行两次排序
- 将一种数据类型的向量复制到同一数据类型的结构向量中的有效方法是什么
- 如何二进制搜索结构向量并在适当的索引处插入
- 结构向量中自定义结构函数的内存使用
- C++11-在结构向量中修改结构的成员
- 我正在尝试找到一个特定的结构成员,该成员是使用 lambda 函数的结构向量
- 在结构向量中搜索一个数据成员,然后打印匹配的所有数据成员