Unique in c++

There is function std::unique() in c++ standard library algorithm.h http://www.cplusplus.com/reference/algorithm/unique/

Which could be used to Removes all but the first element from every consecutive group of equivalent elements in the range [first,last) but I didn’t read about it in c++ documentation and went with the solution given in StackOverflow and assumed that it would be used to remove all the duplicate elements from the container and as name also implies that it does something like that but that information was incomplete as the  std:: unique doesn’t remove all the duplicate elements but it removes the duplicate elements which were adjacent to each other

This was the function which was used to find the duplicate elements and then resize the list.

groupPos.resize( std::distance(groupPos.begin(),std::unique (groupPos.begin(), groupPos.end())));

but it created the problem in my code as I wanted to remove all the duplicate values in list So, I have to change the code little bit and instead of adding all the elements in the list. I have to add the check that same elements key is not on the list again for that I have to add a bool member in the object which was set every time the object’s key was added inside the list and only the objects for which above the bool member was not set were inserted in the list.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s