Jump to content
  • Advertisement
Sign in to follow this  
Alessandro

C++: problem erasing vector items

This topic is 2131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

[source]
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;
};
[/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]
...
return obj.ID!=id;
...
[/source]

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. Edited by Alessandro

Share this post


Link to post
Share on other sites
Advertisement
Are you doing this in the same loop? First you remove everything that doesn't match 0, which leaves only things that match 0. Then you remove everything that doesn't match 1, which means that you get rid of all the 0s that you left in the first pass.

Share this post


Link to post
Share on other sites
Do you really want to call the removal in the loop? Just think about it... if you call "remove this element if its not equal X" for different X the will obviously be no elements left since they would have to be equal to all the different X.

Share this post


Link to post
Share on other sites

Are you doing this in the same loop? First you remove everything that doesn't match 0, which leaves only things that match 0. Then you remove everything that doesn't match 1, which means that you get rid of all the 0s that you left in the first pass.


That's right, I did something stupid. But then what commands can be used that allow to iterate a vector and, at each loop, find a no-match condition and erase it?
I tried using an iterator with the same result...

Share this post


Link to post
Share on other sites
I have no idea what you mean by "find a no-match condition". What exactly do you want to remove from your vector? What exactly do you want left in the vector?

Share this post


Link to post
Share on other sites
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());

Share this post


Link to post
Share on other sites
Sorry for explaining poorly what I intended to do.

Say I have two vector, like:

std::vector<int> someValues;
std::vector<int> someValuesToCheck;

I'd like to remove all the values in someValuesToCheck that are not in someValues.

So that if:

someValues={1,2,5,6,7}
someValuesToCheck={1,3,5,7,8,11}

the resulting someValuesToCheck should have: {1,5,7}

Please tell me if it's not clear. Thanks.

Share this post


Link to post
Share on other sites
It sounds relatively straightforward, in pseudo code: erase(remove_if(someValuesToCheck, not( ismember(someValues)))).

What part is causing you trouble?

Share this post


Link to post
Share on other sites
std::set_intersection can do that.

std::vector<int> results;
std::set_intersection(someValues.begin(), someValues.end(), someValuesToCheck.begin(), someValuesToCheck.end(), std::back_inserter(results));

someValuesToCheck = result;
Edited by Brother Bob

Share this post


Link to post
Share on other sites
try something like this, havent t


std::vector<int> somevalues;
std::vector<int> somevaluesTocheck;
bool status = false;


for(int i = 0 ; i < (int)somevaluesTocheck.size() ; i++){

status = false;

for(int j = 0; j < (int)somevalues.size(); j++){

if(somevaluesTocheck == somevalues[j]){

status = true;

}

}


if(!status){
somevaluesTocheck.erase(somevaluesTocheck.begin() + i);
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!