Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualAlessandro

Posted 22 August 2012 - 10:25 AM

The following code works perfectly when I need to find and remove items in a vector:


for (int i=0; i<NSIZE; i++)

	{

	myVec.erase(std::remove_if(myVec.begin(), myVec.end(), myPred(i)), myVec.end());

	}



// predicate

struct myPred {

	findValue(int i) : id(i) { }

	bool operator()(const myVec::myVecLib &obj) {

		return obj.ID==id;

	}

	int id;

};



However, I'd like to remove the items that don't match. I thought I could do it easily modifying the predicate function like this (showing only the line I changed):


...

	return obj.ID!=id;

...



But in this case the function will actually remove all the items. What am I doing wrong?
Thanks!

Sorry for the code not showing properly, I don't know why but the source=cpp messes things up.

#5Alessandro

Posted 22 August 2012 - 10:24 AM

The following code works perfectly when I need to find and remove items in a vector:


for (int i=0; i<NSIZE; i++)

	{

	myVec.erase(std::remove_if(myVec.begin(), myVec.end(), myPred(i)), myVec.end());

	}



// predicate

struct myPred {

	findValue(int i) : id(i) { }

	bool operator()(const myVec::myVecLib &obj) {

		return obj.ID==id;

	}

	int id;

};



However, I'd like to remove the items that don't match. I thought I could do it easily modifying the predicate function like this (showing only the line I changed):


...

	return obj.ID==id;

...



But in this case the function will actually remove all the items. What am I doing wrong?
Thanks!

Sorry for the code not showing properly, I don't know why but the source=cpp messes things up.

#4Alessandro

Posted 22 August 2012 - 10:23 AM

The following code works perfectly when I need to find and remove items in a vector:


for (int i=0; i<NSIZE; i++)

	{

	myVec.erase(std::remove_if(myVec.begin(), myVec.end(), myPred(i)), myVec.end());

	}



// predicate

struct myPred {

	findValue(int i) : id(i) { }

	bool operator()(const myVec::myVecLib &obj) {

		return obj.ID==id;

	}

	int id;

};



However, I'd like to remove the items that don't match. I thought I could do it easily modifying the predicate function like this (showing only the line I changed):


...

	return obj.ID==id;

...



But in this case the function will actually remove all the items. What am I doing wrong?
Thanks!

#3Alessandro

Posted 22 August 2012 - 10:23 AM

The following code works perfectly when I need to find and remove items in a vector:

[source lang="cpp"]for (int i=0; i<NSIZE; i++) { myVec.erase(std::remove_if(myVec.begin(), myVec.end(), myPred(i)), myVec.end()); }// predicatestruct myPred { findValue(int i) : id(i) { } bool operator()(const myVec::myVecLib &obj) { return obj.ID==id; } int id;};[/source]

However, I'd like to remove the items that don't match. I thought I could do it easily modifying the predicate function like this (showing only the line I changed):

[source lang="cpp"]... return obj.ID==id;...[/source]

But in this case the function will actually remove all the items. What am I doing wrong?
Thanks!

#2Alessandro

Posted 22 August 2012 - 10:22 AM

The following code works perfectly when I need to find and remove items in a vector:


for (int i=0; i<NSIZE; i++)

	{

	myVec.erase(std::remove_if(myVec.begin(), myVec.end(), myPred(i)), myVec.end());

	}



// predicate

struct myPred {

	findValue(int i) : id(i) { }

	bool operator()(const myVec::myVecLib &obj) {

		return obj.ID==id;

	}

	int id;

};



However, I'd like to remove the items that don't match. I thought I could do it easily modifying the predicate function like this (showing only the line I changed):

[source lang="cpp"]... return obj.ID==id;...[/source]

But in this case the function will actually remove all the items. What am I doing wrong?
Thanks!

#1Alessandro

Posted 22 August 2012 - 10:22 AM

The following code works perfectly when I need to find and remove items in a vector:

[source lang="cpp"]for (int i=0; i<NSIZE; i++) { myVec.erase(std::remove_if(myVec.begin(), myVec.end(), myPred(i)), myVec.end()); }// predicatestruct myPred { findValue(int i) : id(i) { } bool operator()(const myVec::myVecLib &obj) { return obj.ID==id; } int id;};[/source]

However, I'd like to remove the items that don't match. I thought I could do it easily modifying the predicate function like this (showing only the line I changed):

[source lang="cpp"]... return obj.ID==id;...[/source]

But in this case the function will actually remove all the items. What am I doing wrong?
Thanks!

PARTNERS