A simple solution
std::vector<int>::iterator it; it = std::unique (myvector.begin(), myvector.end());
This iterator will point to the item next to the last item. You cannot use this iterator if it is not required.
See THIS for further reference.
EDIT:
Since I thought the vector would be sorted, a new solution could be:
vector<int> vec= {5,1,2,3,5,4,2,1,1,4,3,2,4,5,2,1,3,5,2,3,5,2,3,2,3,5,2,1,3}; set<int> s; vector<int>::iterator vecIter=vec.begin(); vector<int>::iterator vecIterCopy; for(;vecIter!=vec.end(); vecIter++) { if(s.find(*vecIter)==s.end()) { s.insert(*vecIter); *vecIterCopy++ = *vecIter; } }
source share