Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualAlessandro

Posted 23 August 2012 - 04:00 PM

What are you actually trying to remove? As the others have said, if you want to erase all but a given ID, then use the following:

int id = /* determine id */;
myVec.erase(std::remove_if(myVec.begin(), myVec.end(), myInversePred(id)), myVec.end());

You can also use the std::not1 predicate to invert an existing predicate:
#include <functional>

int id = /* determine id */;
myVec.erase(std::remove_if(myVec.begin(), myVec.end(), std::not1(myPred(id))), myVec.end());


I'm trying to use the std::not1 as suggested but I've troubles coding the predicate.

Here is the code:

std::vector<myStruct::myStructLib> myVector;

for (int f=0; f<tempValues.size(); f++)
{
  int faceID=tempValues.at(f);
  myVector.erase(std::remove_if(myVector.begin(), myVector.end(), std::not1(myPredicate(faceID))), myVector.end());
}

// predicate
struct myPredicate : std::unary_function<int,bool>{
	bool operator()(const myStruct::myStructLib &obj) {
	  return obj.faceID==id;
	}
	int id;
};


When I compile I get several errors:
error: no type named 'argument_type' in 'struct myPredicate'
error: no match for call to '(std::unary_negate<myPredicate>) (myStruct::myStructLib&)'

#1Alessandro

Posted 23 August 2012 - 03:59 PM

What are you actually trying to remove? As the others have said, if you want to erase all but a given ID, then use the following:

int id = /* determine id */;
myVec.erase(std::remove_if(myVec.begin(), myVec.end(), myInversePred(id)), myVec.end());

You can also use the std::not1 predicate to invert an existing predicate:
#include <functional>

int id = /* determine id */;
myVec.erase(std::remove_if(myVec.begin(), myVec.end(), std::not1(myPred(id))), myVec.end());


I'm trying to use the std::not1 as suggested but I've troubles coding the predicate.

Here is the code:

[source lang="cpp"]std::vector<myStruct::myStructLib> myVector;for (int f=0; f<tempValues.size(); f++){  int faceID=tempValues.at(f);  myVector.erase(std::remove_if(myVector.begin(), myVector.end(), std::not1(myPredicate(faceID))), myVector.end());}// predicatestruct myPredicate : std::unary_function<int,bool>{ bool operator()(const myStruct::myStructLib &obj) {   return obj.faceID==id; } int id;};[/source]

When I compile I get several errors:
error: no type named 'argument_type' in 'struct myPredicate'
error: no match for call to '(std::unary_negate<myPredicate>) (myStruct::myStructLib&)'

PARTNERS