How can I efficiently remove all the duplicates of a stable sorted vector while keeping the last element in order instead of the first?
It's fairly straightforward to keep the first one by doing:
auto it = std::unique( vector.begin(), vector.end() ,[](string a, string b){ return ! (a.compare(b));});
vector.erase(it,vector.end());
But I am not sure how to do the same while keeping the last duplicate element. It can probably be done with reverse iterators,starting the unique search process from the end of the vector but I having trouble combining it with the use of the erase function.
EDIT: I found the solution by modifying the sorting method. Keeping the first duplicate now suffices.
auto it = std::unique( vector.rbegin(), vector.rend() ,[](string a, string b){ return ! (a.compare(b));});
vector.erase(vector.begin(),it.base());