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

Pey*_*Pey -1 c++ vector erase

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

例如,这里有两个向量:

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)

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

Tho*_*ews 6

我建议你改变你的数据结构.使用结构来保存这两个元素:

struct Entry
{
  std::string text;
  double      value;
};
Run Code Online (Sandbox Code Playgroud)

现在这成为两个元素的一个向量:
std::vector<Entry> first_and_second;

在向量中搜索给定文本时,可以删除包含文本和值的一个元素.