gpt4 book ai didi

c++ - 移除 vector 元素使用 vector 中的条件

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:57 25 4
gpt4 key购买 nike

我有两个大小相同的 vector a 和 b。

vector<int>  a{    4,    3,   1,    3,   1};
vector<bool> b{false,false,true,false,true};

如果 b 中的相同元素(相同索引)为真,我想删除 a 中的元素。

应用函数后:a = 4,3,3

注意:我想使用 std 算法或函数而不是简单的 for 循环。

最佳答案

  std::vector<int> v {1,2,3,4,5,6};
std::vector<bool> b {true, false, true, false, true, false};

v.erase(std::remove_if(v.begin(), v.end(),
[&b, &v](int const &i) { return b.at(&i - v.data()); }), v.end());

LIVE DEMO

关于c++ - 移除 vector 元素使用 vector<bool> 中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33494364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com