我要删除一些内容Val[i]
,如下所示:
struct Sstruct{
int v1;
double v2;
};
struct Sstruct2{
std::vector<int> id;
double a;
std::vector<Sstruct > b;
};
std::vector <Sstruct2> Val;
我尝试了这段代码,但使用时出现错误 std::remove_if
bool TestFun(Sstruct2 id1)
{
bool result= true;
if ((id1.a< somevalue)
{
// fails
result= false;
}
return result;
}
void DelFun()
{
for (int i= 0; i< Val.size(); i++)
{
if (!TestFun(Val[i]))
{
**// here i don't now how to search for Val[i] that fails in the condition**
Val.erase(std::remove_if(Val.begin(), Val.end(),
Val[i].id.begin()), Val.end());
}
}
}
错误: C2064:术语未求值为带有1个参数的函数
您不必使用循环循环,只需在 DelFun
Val.erase(std::remove_if(Val.begin(), Val.end(),
[]( const Sstruct2& id)
{ // Lambda C++11 use flag -std=c++11
return ( id1.a < somevalue ) ;
}
Val.end());
// Or without Lambda
struct TestFun
{
bool operator()(const Sstruct2& i) const
{
return ( id1.a < somevalue ) ;
}
};
Val.erase(std::remove_if(Val.begin(), Val.end(),
TestFun()
Val.end());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句