小编Pey*_*Pey的帖子

根据第一个向量的元素从两个std :: vectors中删除元素

我必须使用相同数量的元素.我想基于条件删除第一个向量的元素,但我还想从第二个向量中删除位于相同位置的元素.

例如,这里有两个向量:

std::vector<std::string> first = {"one", "two", "one", "three"}
std::vector<double> second = {15.18, 14.2, 2.3, 153.3}
Run Code Online (Sandbox Code Playgroud)

我想要的是基于条件删除元素是否为"一".最终结果是:

std::vector<std::string> first = {"two", "three"}
std::vector<double> second = {14.2, 153.3}
Run Code Online (Sandbox Code Playgroud)

我可以first通过使用以下方法删除元素:

bool pred(std::string name) {
  return name == "one";
}

void main() {

  std::vector<std::string> first = {"one", "two", "one", "three"}
  first.erase(first.begin(), first.end(), pred);

}
Run Code Online (Sandbox Code Playgroud)

但我也不知道从第二个向量中删除元素的方法.

c++ vector erase

-1
推荐指数
1
解决办法
641
查看次数

标签 统计

c++ ×1

erase ×1

vector ×1