For remoovingremoving duplicates you can use std::unique here is the description of the function. It will return An iterator to the element that follows the last element not removed."An iterator to the element that follows the last element not removed". So you can resize your string using a.resize(i) where i is the return value of std::unique.