Jump to content
  • Advertisement
Sign in to follow this  
Alessandro

c++: std::erase, std::unique

This topic is 2472 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

I have a vector sorted for a mat variable, and I'd like to find and erase duplicates.

I have this code and I can't understand why it doesn't make the job (it doesn't remove the duplicates):


bool compareMat (const myObjects::myObjectLib & lhs, const myObjects::myObjectLib & rhs) {
return (lhs.mat==rhs.mat);
}

myFullHair.erase(std::unique(myFullHair.begin(), myFullHair.end(), compareMat));


Should be so simple, the unique test if mat matches and should erase it. wacko.png

Share this post


Link to post
Share on other sites
Advertisement
The version of erase you are using will only erase one element.
Secondly, unique requires a sorted range, IIRC.

Share this post


Link to post
Share on other sites

Secondly, unique requires a sorted range, IIRC.

Yes, it does. It only removes sequences of duplicates.

So if 'a' appears twice, but the two are not by each other, unique() will leave both of them. If, however, they are by each other, then it will only leave one. So yes, Alessandro, you should sort() before you unique(), and then erase() from the iterator returned by unique() to the end() of the data structure.

Share this post


Link to post
Share on other sites
Hidden
My guess is for an unsorted vector your best bet would be:

myFullHair.erase(parition(myFullHair.begin(),myFullHair.end(),compareMat),myFullHair.end());

Share this post


Link to post

My guess is for an unsorted vector your best bet would be:

myFullHair.erase(parition(myFullHair.begin(),myFullHair.end(),compareMat),myFullHair.end());

partition doesn't help get rid of duplicate elements though. Heck, it doesn't even test for duplicates in any way. For partition, compareMat only takes one parameter, not two, so you certainly aren't comparing two items.

Share this post


Link to post
Share on other sites

should be so simple, the unique test if mat matches and should erase it.


If your vector is *sorted*, your code appears correct to me, but depending on how you store your objects in the vector you might want to override the operator== for your class.

Also it might be worth to think about using an associative container like set or map instead of a vector if your are prepared to live with the disatvantages of these containers.

If you are able to insert the objects sorted, you might be able to cheaply check for duplicates when pushing your objects into the vector.

Share this post


Link to post
Share on other sites

If your vector is *sorted*, your code appears correct to me, but depending on how you store your objects in the vector you might want to override the operator== for your class.

No, it's not. As has already been mentioned, when using std::unique() you need to erase from the iterator returned by unique() to the end of the range. He's only erasing the iterator returned by unique().

Share this post


Link to post
Share on other sites

[quote name='doeme' timestamp='1330955624' post='4919443']
If your vector is *sorted*, your code appears correct to me, but depending on how you store your objects in the vector you might want to override the operator== for your class.

No, it's not. As has already been mentioned, when using std::unique() you need to erase from the iterator returned by unique() to the end of the range. He's only erasing the iterator returned by unique().
[/quote]

You are right. Sorry, my mistake.

Share this post


Link to post
Share on other sites

Ok, so what's the solution? Iterate each element and do erase+unique?[/quote]



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

No, just use the two parameter version of erase.[/font]



[color=#000000]myFullHair.erase(std::unique(myFullHair.begin(), myFullHair.end(), compareMat),myFullHair.end());

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!