Removing duplicate characters from string using STL

Is there a way to remove duplicate characters from a string, how can they be removed from vectors as shown below

sort( vec.begin(), vec.end() );
vec.erase( unique( vec.begin(), vec.end() ), vec.end() );

Or do I just need to create a basic solution for it? What I thought:

I could add all the characters to the set

+4
source share
1 answer

The whole point of the C ++ algorithm and container design is that the algorithms are - as far as possible - agnostic of the container.

So the same algorithm works that works on vectors! - line by line.

std::sort(str.begin(), str.end());
str.erase(std::unique(str.begin(), str.end()), str.end());

C - , erase , , ( begin end -, youd ).

+11

All Articles