C++ 如何在一定条件下删除向量中的所有元组?

C++ 如何在一定条件下删除向量中的所有元组?,c++,vector,tuples,C++,Vector,Tuples,我有一个3元组的向量。我想删除第二个值为4的所有元组。这是我的代码: int main() { tuple thing1 = make_tuple(1, 4, 2, 2); tuple thing2 = make_tuple(2, 2, 2, 2); tuple thing3 = make_tuple(3, 4, 2, 2); vector<thing> things = {thing1, thing2, thing3}; int ind

我有一个3元组的向量。我想删除第二个值为4的所有元组。这是我的代码:

int main() {

    tuple thing1 = make_tuple(1, 4, 2, 2);
    tuple thing2 = make_tuple(2, 2, 2, 2);
    tuple thing3 = make_tuple(3, 4, 2, 2);

    vector<thing> things = {thing1, thing2, thing3};

    int index = 0;
    for (vector<thing>::iterator it = things.begin(); it != things.end(); ++it) {
        if (get<1>(*it) == 4) {
            things.erase(things.begin()+index);
        } else {
            index++;
        }
    }
}
但是这段代码删除了所有这些。有人能帮我吗?非常感谢您:

答案已被采纳。使用remove_if函数模板可以执行以下操作:

things.erase(std::remove_if(
things.begin(), things.end(),
[](const thing& x) -> bool{ 
    return get<1>(x) == 4; // put your condition here
}), things.end());

见一个C++外壳。

< P>答案采用。使用remove_if函数模板可以执行以下操作:

things.erase(std::remove_if(
things.begin(), things.end(),
[](const thing& x) -> bool{ 
    return get<1>(x) == 4; // put your condition here
}), things.end());

参见一个C++外壳。

可以使用擦除删除习惯。你既有索引又有它,但是当擦除发生时它们会被取消同步。你可以使用擦除删除习惯用法。你既有索引也有它,但是当发生擦除时它们会被同步。