C++: problem erasing vector items

Started by
16 comments, last by Alessandro 11 years, 7 months ago
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.
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.
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.

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...
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?
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());
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.
It sounds relatively straightforward, in pseudo code: erase(remove_if(someValuesToCheck, not( ismember(someValues)))).

What part is causing you trouble?
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;
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);
}
}

This topic is closed to new replies.

Advertisement