I am trying to figure out some operations of algorithm in C++. One of them is unique and its return value. Quoting from cplusplus.com:
An iterator to the element that follows the last element not removed.
The range between first and this iterator includes all the elements in the sequence that were not considered duplicates.
To check it, I wrote a script in C++ as following:
1. first std::sort the container - because std::unique checks two consecutive elements for equivalence; if you have two equivalent elements that are not consecutive, the latter won’t be removed.
2. then std:: unique
3. finally std::erase because std::unique doesn't actually remove the elements, instead they are copied to the end
Thank you for you explanations. I've got the point: The return value is
An iterator to the element that follows the last element not removed.
The range between first and this iterator includes all the elements in the sequence that were not considered duplicates.
.
Therefore I the resize() member function I should have set pt1-vrr.begin() instead of pt1-vrr.begin()+1. Using distance(vrr.begin(),pt1) is also an option for it.
OP: Did you try out first what you've just proposed? I don't think so, otherwise you'd have seen that the modified vector is now {1, 2, 1}.
You'd either have to use a set as Handoro said or sort the vector first as I did