我有一个带有3个元组的 vector 。我想删除第二个值为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++;
}
}
}
但是这段代码删除了所有这些代码。有人可以帮我吗?谢谢你这么糊涂:)
最佳答案
答案来自std::vector removing elements which fulfill some conditions。使用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++ Shell上查看example。
关于c++ - 如何删除具有特定条件的 vector 中的所有元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59337911/